How to write this in ruby

Discussion in 'Ruby' started by Alex G., Oct 30, 2010.

  1. Alex G.

    Alex G. Guest

    /**

    So i need to make this java application into a ruby application, but im
    very new to ruby (first day). I played around a bit and made a couple of
    server apps.. hello server world.. and a simple chat, but i need
    something where the server takes information from one client, and passes
    it to a second client, after making a few changes.

    Altho not shown in the code this application is a server for a game of
    checkers, it takes the move that player 1 makes, inverts it and sends it
    to player 2.

    i been reading around on how to do this, if someone can help or point to
    where i should focus my research, i would be greatful.

    */

    import java.net.*;
    import java.io.*;

    public class GameServer{

    private Player players[];
    private ServerSocket server;
    private int currentPlayer;

    public GameServer()
    {

    players = new Player[ 2 ];
    currentPlayer = 0;

    // set up ServerSocket
    try {
    server = new ServerSocket( 5000, 2 );
    }
    catch( IOException e ) {
    e.printStackTrace();
    System.exit( 1 );
    }
    }
    public synchronized void changeTurn(int player, int x, int a, int b,
    int c, int d){

    while ( player != currentPlayer ) {
    try {
    wait();
    }
    catch( InterruptedException e ) {
    e.printStackTrace();
    }
    System.out.println("waiting...");
    }
    currentPlayer = ( currentPlayer + 1 ) % 2;
    players[ currentPlayer ].otherPlayerMoved(x,a,b,c,d);
    if(x==0){
    currentPlayer = ( currentPlayer + 1 ) % 2;
    }

    notify(); // tell waiting player to continue

    }


    // wait for two connections so game can be played
    public void execute()
    {
    for ( int i = 0; i < players.length; i++ ) {
    try {
    players[ i ] =
    new Player( server.accept(), this, i );
    players[ i ].start();
    }
    catch( IOException e ) {
    e.printStackTrace();
    System.exit( 1 );
    }
    }

    // Player X is suspended until Player O connects.
    // Resume player X now.
    synchronized ( players[ 0 ] ) {
    players[ 0 ].threadSuspended = false;
    players[ 0 ].notify();
    }

    }


    public boolean gameOver()
    {
    // Place code here to test for a winner of the game
    return false;
    }

    public static void main( String args[] )
    {
    GameServer game = new GameServer();
    game.execute();
    }
    }

    // Player class to manage each Player as a thread
    class Player extends Thread {
    private Socket connection;
    private DataInputStream input;
    private DataOutputStream output;
    private GameServer control;
    private int playerIndex;
    private char mark;
    protected boolean threadSuspended = true;

    public Player( Socket s, GameServer t, int num )
    {
    mark = ( num == 0 ? 'X' : 'Y' );

    connection = s;

    try {
    input = new DataInputStream(
    connection.getInputStream() );
    output = new DataOutputStream(
    connection.getOutputStream() );
    }
    catch( IOException e ) {
    e.printStackTrace();
    System.exit( 1 );
    }

    control = t;
    playerIndex = num;
    }

    public void otherPlayerMoved(int x, int a, int b, int c, int d)
    {
    try {
    output.writeUTF( "Opponent moved" );
    output.writeInt(x);
    output.writeInt(a);
    output.writeInt(b);
    output.writeInt(c);
    output.writeInt(d);
    }
    catch ( IOException e ) {
    e.printStackTrace();
    }
    }

    public void run()
    {
    boolean done = false;

    try {
    output.writeChar( mark );
    output.writeUTF( "Player " +
    ( playerIndex == 0 ? "X connected\n" :
    "Y connected" ) );

    // wait for another player to arrive
    if ( mark == 'X') {
    output.writeUTF( "Waiting for another player" );

    try {
    synchronized( this ) {
    while ( threadSuspended )
    wait();
    }
    }
    catch ( InterruptedException e ) {
    e.printStackTrace();
    }

    output.writeUTF(
    "Other player connected. Your move." );
    }

    // Play game
    while ( !done ) {
    int x = input.readInt();
    int a = input.readInt();
    int b = input.readInt();
    int c = input.readInt();
    int d = input.readInt();

    control.changeTurn(playerIndex,x,a,b,c,d);


    if ( control.gameOver() ){
    done = true;
    }
    }

    connection.close();
    }
    catch( IOException e ) {
    e.printStackTrace();
    System.exit( 1 );
    }
    }
    }

    --
    Posted via http://www.ruby-forum.com/.
     
    Alex G., Oct 30, 2010
    #1
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. =?Utf-8?B?QWJlbCBDaGFu?=
    Replies:
    6
    Views:
    9,445
    Rahul
    May 3, 2006
  2. Peter Hansen
    Replies:
    1
    Views:
    398
    Joona I Palaste
    Aug 24, 2003
  3. JasBascom
    Replies:
    1
    Views:
    371
    John Harrison
    Feb 24, 2004
  4. anne001
    Replies:
    1
    Views:
    525
  5. estherschindler
    Replies:
    6
    Views:
    153
Loading...

Share This Page