User Name Password Register
DaniWeb IT Discussion Community
All
What is DaniWeb IT Discussion Community?
You're currently browsing the Perl section within the Software Development category of DaniWeb, a massive community of 374,155 software developers, web developers, Internet marketers, and tech gurus who are all enthusiastic about making contacts, networking, and learning from each other. In fact, there are 3,439 IT professionals currently interacting right now! Registration is free, only takes a minute and lets you enjoy all of the interactive features of the site.
Please support our Perl advertiser:
Views: 580 | Replies: 8
Reply
Join Date: May 2008
Posts: 4
Reputation: wizard_bro is an unknown quantity at this point 
Rep Power: 0
Solved Threads: 0
wizard_bro wizard_bro is offline Offline
Newbie Poster

Tic Tac Toe

  #1  
May 1st, 2008
I'm developing this game with sockets, server-client connection!!
And the problem is I can't put the game work, so many errors along the program, can anyone help me?!
It´s urgent....
AddThis Social Bookmark Button
Reply With Quote  
Join Date: Mar 2006
Posts: 560
Reputation: KevinADC is an unknown quantity at this point 
Rep Power: 4
Solved Threads: 30
KevinADC's Avatar
KevinADC KevinADC is offline Offline
Posting Pro

Re: Tic Tac Toe

  #2  
May 1st, 2008
There is no urgent help here. All questions have the same priority: none.

If you want help you need to post your code, describe in detail the problems you are having and post any error messages you are getting.

Then if someone wants to help you they will.
Reply With Quote  
Join Date: May 2008
Posts: 4
Reputation: wizard_bro is an unknown quantity at this point 
Rep Power: 0
Solved Threads: 0
wizard_bro wizard_bro is offline Offline
Newbie Poster

Re: Tic Tac Toe

  #3  
May 1st, 2008
I'm developing 2 programs, with sockets: tic tac toe server and tic tac toe client. The first part was easy, but when the connection starts, so many problems. I put right here the 2 programs, then someone can make this much better!! Please?!

Client:
#! /usr/bin/perl -w

# Tic tac toe

use strict;
use Socket;

my $host = shift || 'localhost';
my $port = shift || 7890;
my $proto = getprotobyname('tcp');

my $line;
my $end = 0;

my $iaddr = inet_aton($host);
my $paddr = sockaddr_in($port, $iaddr);

socket(SOCKET, PF_INET, SOCK_STREAM, $proto) or die "socket: $!";

my $old_fh = select(SOCKET);
$| = 1;
select($old_fh);

connect(SOCKET, $paddr) or die "connect: $!";
print "Client connected \n";
print "Data received from server: \n";
print ("Do you wish to play tic tac toe? Please answer Y or N\n");

$Answer = <STDIN>;

chomp($Answer);

if ( $Answer eq "N" || $Answer eq "n") {
print ("Ok, goodbye!\n");
}
if ( $Answer ne "N" && $Answer ne "n" && $Answer ne "Y" && $Answer ne "y") {

print ("Give me a yes or no answer next time, please. Goodbye!\n");

}

if ( $Answer eq "Y" || $Answer eq "y") {

@Board = (" ", " ", " ", " ", " ", " ", " ", " ", " ");
@BestMoves = (5, 1, 3, 7, 9);

print ("You will be X.\n");
print ("You will be first!\n");

$Winner = 0;

while ( $Winner != 1 )
{
$Answer = GetserverMove();
$Position = $Answer - 1;

$Board[$Position] = "X";

DisplayBoard();

$Winner = CheckForWinner();

if ($Winner >=1)

$Answer = GetclientMove();

$Position = $Answer - 1;

$Board[$Position] = "O";

DisplayBoard();

$Winner = CheckForWinner();

if ($Winner >= 1)

}
if ($Winner ==1) {print ("X is a winner!!! \a\a\a\n")};

if ($Winner ==2) {print ("O is a winner!!! \n")};

}

}

sub DisplayBoard
{
print ("------------TOP----------------\n");

print ("|$Board[0]|$Board[1]|$Board[2]|\n");

print ("|$Board[3]|$Board[4]|$Board[5]|\n");

print ("|$Board[6]|$Board[7]|$Board[8]|\n");

print ("----------BOTTOM---------------\n");


}

sub GetserverMove
{
$MyMove = 0;

foreach $i (@BestMoves) {

if ( $Board[$i-1] eq " " ) {

$MyMove = $i;


}

}

if ($MyMove != 0) {

return $MyMove};

for ($i = 1; $i<=9 ; $i++) {

if ( $Board[$i-1] eq " " ) {

$MyMove = $i;

}

}

return $MyMove;

}

sub GetclientMove {


$OkMove = 0;



while ($OkMove == 0) {


print ("What is your next move? Please enter 1-9\n");

$i = <STDIN>;

chomp($i);



if ($i > 0 && $i < 10) {



if ( $Board[$i-1] ne " ") {

$OkMove = 0;

print ("Bad Move! Give me a better one!\n");

DisplayBoard();

} else {



$OkMove = 1;

$MyMove = $i;

}

} else {



$OkMove = 0;

print ("Number must be 1 through 9!\n");

}

}

return $MyMove;

}

sub CheckForWinner {



if ($Board[0] eq "X" && $Board[1] eq "X" && $Board[2] eq "X" ){return 1};

if ($Board[3] eq "X" && $Board[4] eq "X" && $Board[5] eq "X" ){return 1};

if ($Board[6] eq "X" && $Board[7] eq "X" && $Board[8] eq "X" ){return 1};

if ($Board[0] eq "X" && $Board[3] eq "X" && $Board[6] eq "X" ){return 1};

if ($Board[1] eq "X" && $Board[4] eq "X" && $Board[7] eq "X" ){return 1};

if ($Board[2] eq "X" && $Board[5] eq "X" && $Board[8] eq "X" ){return 1};

if ($Board[0] eq "X" && $Board[4] eq "X" && $Board[8] eq "X" ){return 1};

if ($Board[2] eq "X" && $Board[4] eq "X" && $Board[6] eq "X" ){return 1};



if ($Board[0] eq "O" && $Board[1] eq "O" && $Board[2] eq "O" ){return 2};

if ($Board[3] eq "O" && $Board[4] eq "O" && $Board[5] eq "O" ){return 2};

if ($Board[6] eq "O" && $Board[7] eq "O" && $Board[8] eq "O" ){return 2};

if ($Board[0] eq "O" && $Board[3] eq "O" && $Board[6] eq "O" ){return 2};

if ($Board[1] eq "O" && $Board[4] eq "O" && $Board[7] eq "O" ){return 2};

if ($Board[2] eq "O" && $Board[5] eq "O" && $Board[8] eq "O" ){return 2};

if ($Board[0] eq "O" && $Board[4] eq "O" && $Board[8] eq "O" ){return 2};

if ($Board[2] eq "O" && $Board[4] eq "O" && $Board[6] eq "O" ){return 2};

}

close SOCKET or die "close: $!";
print "\n --Client has disconnected-- \n";

---------------------------------------------------------------------------------------------

Server:

#! /usr/bin/perl -w


# Tic tac toe

use strict;
use Socket;

my $port = shift || 7890;
my $proto = getprotobyname('tcp');

socket(SERVER, PF_INET, SOCK_STREAM, $proto) or die "socket: $!";
setsockopt(SERVER, SOL_SOCKET, SO_REUSEADDR, 1) or die "setsock: $!";

my $paddr = sockaddr_in($port, INADDR_ANY);

bind(SERVER, $paddr) or die "bind: $!";
listen(SERVER, SOMAXCONN) or die "listen: $!";
print "SERVER started on port $port \n";

my $client_addr;


while ($client_addr = accept(CLIENT, SERVER))
{
my $old_fh = select(CLIENT);
$| = 1;
select($old_fh);

print "New connection established.\n";
print ("Do you wish to play first? Answer Y or N\n");

$Answer = <STDIN>;
chomp($Answer);

if ( $Answer eq "N" || $Answer eq "n")
{
print ("Ok, goodbye!\n");
}

if ( $Answer "Y" || $Answer eq "y")
{
@Board = (" ", " ", " ", " ", " ", " ", " ", " ", " ");
@BestMoves = (5, 1, 3, 7, 9);

print ("You will be X.\n");
print ("You will be first!\n");

$Winner = 0;

while ( $Winner != 1 )
{
$Answer = GetclientMove();
$Position = $Answer - 1;

$Board[$Position] = "X";

DisplayBoard();

$Winner = CheckForWinner();

if ($Winner >=1)

$Answer = GetserverMove();

$Position = $Answer - 1;

$Board[$Position] = "O";

DisplayBoard();

$Winner = CheckForWinner();

if ($Winner >= 1)

}
if ($Winner ==1) {print ("X is a winner!!! \a\a\a\n")};

if ($Winner ==2) {print ("O is a winner!!! \n")};

}

}

sub DisplayBoard
{
print ("------------TOP----------------\n");

print ("|$Board[0]|$Board[1]|$Board[2]|\n");

print ("|$Board[3]|$Board[4]|$Board[5]|\n");

print ("|$Board[6]|$Board[7]|$Board[8]|\n");

print ("----------BOTTOM---------------\n");


}

sub GetclientMove
{
$MyMove = 0;

foreach $i (@BestMoves) {

if ( $Board[$i-1] eq " " ) {

$MyMove = $i;


}

}

if ($MyMove != 0) {

return $MyMove};

for ($i = 1; $i<=9 ; $i++) {

if ( $Board[$i-1] eq " " ) {

$MyMove = $i;

}

}

return $MyMove;

}

sub GetserverMove {


$OkMove = 0;



while ($OkMove == 0) {


print ("What is your next move? Please enter 1-9\n");

$i = <STDIN>;

chomp($i);



if ($i > 0 && $i < 10) {



if ( $Board[$i-1] ne " ") {

$OkMove = 0;

print ("Bad Move! Give me a better one!\n");

DisplayBoard();

} else {



$OkMove = 1;

$MyMove = $i;

}

} else {



$OkMove = 0;

print ("Number must be 1 through 9!\n");

}

}

return $MyMove;

}

sub CheckForWinner {



if ($Board[0] eq "X" && $Board[1] eq "X" && $Board[2] eq "X" ){return 1};

if ($Board[3] eq "X" && $Board[4] eq "X" && $Board[5] eq "X" ){return 1};

if ($Board[6] eq "X" && $Board[7] eq "X" && $Board[8] eq "X" ){return 1};

if ($Board[0] eq "X" && $Board[3] eq "X" && $Board[6] eq "X" ){return 1};

if ($Board[1] eq "X" && $Board[4] eq "X" && $Board[7] eq "X" ){return 1};

if ($Board[2] eq "X" && $Board[5] eq "X" && $Board[8] eq "X" ){return 1};

if ($Board[0] eq "X" && $Board[4] eq "X" && $Board[8] eq "X" ){return 1};

if ($Board[2] eq "X" && $Board[4] eq "X" && $Board[6] eq "X" ){return 1};



if ($Board[0] eq "O" && $Board[1] eq "O" && $Board[2] eq "O" ){return 2};

if ($Board[3] eq "O" && $Board[4] eq "O" && $Board[5] eq "O" ){return 2};

if ($Board[6] eq "O" && $Board[7] eq "O" && $Board[8] eq "O" ){return 2};

if ($Board[0] eq "O" && $Board[3] eq "O" && $Board[6] eq "O" ){return 2};

if ($Board[1] eq "O" && $Board[4] eq "O" && $Board[7] eq "O" ){return 2};

if ($Board[2] eq "O" && $Board[5] eq "O" && $Board[8] eq "O" ){return 2};

if ($Board[0] eq "O" && $Board[4] eq "O" && $Board[8] eq "O" ){return 2};

if ($Board[2] eq "O" && $Board[4] eq "O" && $Board[6] eq "O" ){return 2};

}


close CLIENT;
print "--END--";
exit;
Last edited by wizard_bro : May 1st, 2008 at 5:25 pm.
Reply With Quote  
Join Date: May 2008
Posts: 4
Reputation: wizard_bro is an unknown quantity at this point 
Rep Power: 0
Solved Threads: 0
wizard_bro wizard_bro is offline Offline
Newbie Poster

Re: Tic Tac Toe

  #4  
May 1st, 2008
the game must do this:
-the client connects;
-then the server go and wait for the call;
-the client waits for the server's moves,
-the program prints the game (board, positions);
-server plays, while the client waits for his time;
-the game prints the board, positions, moves;
-then the client plays while server waits for his time;

this will go to this cycle until someone decides to finish the game!!

so, anyone has the answer for my problem?!
Reply With Quote  
Join Date: Mar 2006
Posts: 560
Reputation: KevinADC is an unknown quantity at this point 
Rep Power: 4
Solved Threads: 30
KevinADC's Avatar
KevinADC KevinADC is offline Offline
Posting Pro

Re: Tic Tac Toe

  #5  
May 2nd, 2008
As said before: describe in details the problems you are having.
Reply With Quote  
Join Date: May 2008
Posts: 4
Reputation: wizard_bro is an unknown quantity at this point 
Rep Power: 0
Solved Threads: 0
wizard_bro wizard_bro is offline Offline
Newbie Poster

Re: Tic Tac Toe

  #6  
May 2nd, 2008
my bad, but can u help me?!
Reply With Quote  
Join Date: Jan 2006
Posts: 215
Reputation: katharnakh is an unknown quantity at this point 
Rep Power: 3
Solved Threads: 19
katharnakh's Avatar
katharnakh katharnakh is offline Offline
Posting Whiz in Training

Re: Tic Tac Toe

  #7  
May 2nd, 2008
Originally Posted by wizard_bro View Post
...
And the problem is I can't put the game work, so many errors along the program, can anyone help me?!
It´s urgent....

you have posted your code, what errors you are getting? post that too... it would be easy to tackle problem.
challenge the limits
Reply With Quote  
Join Date: Jan 2006
Posts: 215
Reputation: katharnakh is an unknown quantity at this point 
Rep Power: 3
Solved Threads: 19
katharnakh's Avatar
katharnakh katharnakh is offline Offline
Posting Whiz in Training

Re: Tic Tac Toe

  #8  
May 2nd, 2008
Also go through this link http://www.daniweb.com/forums/misc.php?do=bbcode#code before posting your code.
challenge the limits
Reply With Quote  
Join Date: Mar 2006
Posts: 560
Reputation: KevinADC is an unknown quantity at this point 
Rep Power: 4
Solved Threads: 30
KevinADC's Avatar
KevinADC KevinADC is offline Offline
Posting Pro

Re: Tic Tac Toe

  #9  
May 2nd, 2008
Originally Posted by wizard_bro View Post
my bad, but can u help me?!


I will try and help you with specific problems but so far you have not provided that information.
Reply With Quote  
Reply

Only community members can participate in forum threads. You must register or log in to contribute.

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)

 

DaniWeb Perl Marketplace
Thread Tools Display Modes

Similar Threads
Other Threads in the Perl Forum

All times are GMT -4. The time now is 3:29 am.
Forum system based on vBulletin Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
©2003 - 2008 DaniWeb® LLC