How to write this in ruby

A

Alex G.

/**

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 );
}
}
}
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,743
Messages
2,569,478
Members
44,898
Latest member
BlairH7607

Latest Threads

Top