Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Ruby > How to write this in ruby

Reply
Thread Tools

How to write this in ruby

 
 
Alex G.
Guest
Posts: n/a
 
      10-30-2010
/**

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/.

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Re: How include a large array? Edward A. Falk C Programming 1 04-04-2013 08:07 PM
Zed Shaw on Ruby Pros/Cons: You Used Ruby to Write WHAT? estherschindler Ruby 6 03-04-2008 10:52 PM
How to use Response.write to write to a specific area on a aspx pa =?Utf-8?B?QWJlbCBDaGFu?= ASP .Net 6 05-03-2006 10:16 PM
#!/usr/bin/ruby , #!/usr/bin/ruby -w , #!/usr/bin/ruby -T?, #!/usr/bin/ruby -T1... anne001 Ruby 1 04-23-2006 03:02 PM
DVD Write speed - Not able to write at 8 speed Vincent Wonnacott Computer Support 1 09-16-2004 03:14 PM



Advertisments