Network Tic-tac-Toe
Author |
Message |
maiku

|
Posted: Sun Apr 23, 2006 1:22 am Post subject: Network Tic-tac-Toe |
|
|
I'm a TOTAL programming noob.....
I'm only in grade 10, in computer info and sci class, and the only teacher who could have probably helped me left the school!
I got bored and tried to make tic-tac-toe for 2 computers. The only error is whenever the program sends an array over the network.....Here's my code
code: | setscreen ("graphics:250;250,nobuttonbar")
var netAddress : string
var stream, choice : int
var x, y, b, turn, font, win : int
var spot : array 1 .. 9 of int
win := 0
turn := 1
font := Font.New ("Times New Roman:40")
for i : 1 .. 9
spot (i) := 0
end for
procedure switchTurn
if turn = 1 then
turn := 2
elsif turn = 2 then
turn := 1
end if
end switchTurn
procedure XorO
for i : 1 .. 9
put : stream, spot (i)
end for
Mouse.Where (x, y, b)
if b = 1 then
if x > 10 and x < 59 and y > 10 and y < 59 and spot (1) = 0 then
spot (1) := turn
if turn = 1 then
Draw.Text ("X", 15, 15, font, brightred)
elsif turn = 2 then
Draw.Text ("O", 15, 15, font, brightgreen)
end if
switchTurn
elsif x > 61 and x < 109 and y > 10 and y < 59 and spot (2) = 0 then
spot (2) := turn
if turn = 1 then
Draw.Text ("X", 65, 15, font, brightred)
elsif turn = 2 then
Draw.Text ("O", 65, 15, font, brightgreen)
end if
switchTurn
elsif x > 111 and x < 160 and y > 10 and y < 59 and spot (3) = 0 then
spot (3) := turn
if turn = 1 then
Draw.Text ("X", 115, 15, font, brightred)
elsif turn = 2 then
Draw.Text ("O", 115, 15, font, brightgreen)
end if
switchTurn
elsif x > 10 and x < 59 and y > 61 and y < 159 and spot (4) = 0 then
spot (4) := turn
if turn = 1 then
Draw.Text ("X", 15, 65, font, brightred)
elsif turn = 2 then
Draw.Text ("O", 15, 65, font, brightgreen)
end if
switchTurn
elsif x > 61 and x < 109 and y > 61 and y < 159 and spot (5) = 0 then
spot (5) := turn
if turn = 1 then
Draw.Text ("X", 65, 65, font, brightred)
elsif turn = 2 then
Draw.Text ("O", 65, 65, font, brightgreen)
end if
switchTurn
elsif x > 111 and x < 160 and y > 61 and y < 159 and spot (6) = 0 then
spot (6) := turn
if turn = 1 then
Draw.Text ("X", 115, 65, font, brightred)
elsif turn = 2 then
Draw.Text ("O", 115, 65, font, brightgreen)
end if
switchTurn
elsif x > 10 and x < 59 and y > 109 and y < 160 and spot (7) = 0 then
spot (7) := turn
if turn = 1 then
Draw.Text ("X", 15, 115, font, brightred)
elsif turn = 2 then
Draw.Text ("O", 15, 115, font, brightgreen)
end if
switchTurn
elsif x > 61 and x < 109 and y > 109 and y < 160 and spot (8) = 0 then
spot (8) := turn
if turn = 1 then
Draw.Text ("X", 65, 115, font, brightred)
elsif turn = 2 then
Draw.Text ("O", 65, 115, font, brightgreen)
end if
switchTurn
elsif x > 111 and x < 160 and y > 109 and y < 160 and spot (9) = 0 then
spot (9) := turn
if turn = 1 then
Draw.Text ("X", 115, 115, font, brightred)
elsif turn = 2 then
Draw.Text ("O", 115, 115, font, brightgreen)
end if
switchTurn
end if
end if
end XorO
procedure receive
loop
if Net.CharAvailable (stream) then
for i : 1 .. 9
get : stream, spot (i)
end for
if spot (1) > 0 then
if spot (1) = 1 then
Draw.Text ("X", 15, 15, font, brightred)
elsif spot (1) = 2 then
Draw.Text ("0", 15, 15, font, brightgreen)
end if
switchTurn
end if
if spot (2) > 0 then
if spot (2) = 1 then
Draw.Text ("X", 65, 15, font, brightred)
elsif spot (2) = 2 then
Draw.Text ("0", 65, 15, font, brightgreen)
end if
switchTurn
end if
if spot (3) > 0 then
if spot (3) = 1 then
Draw.Text ("X", 115, 15, font, brightred)
elsif spot (3) = 2 then
Draw.Text ("0", 115, 15, font, brightgreen)
end if
switchTurn
end if
if spot (4) > 0 then
if spot (4) = 1 then
Draw.Text ("X", 15, 65, font, brightred)
elsif spot (4) = 2 then
Draw.Text ("0", 15, 65, font, brightgreen)
end if
switchTurn
end if
if spot (5) > 0 then
if spot (5) = 1 then
Draw.Text ("X", 65, 65, font, brightred)
elsif spot (5) = 2 then
Draw.Text ("0", 65, 65, font, brightgreen)
end if
switchTurn
end if
if spot (6) > 0 then
if spot (6) = 1 then
Draw.Text ("X", 115, 65, font, brightred)
elsif spot (6) = 2 then
Draw.Text ("0", 115, 65, font, brightgreen)
end if
switchTurn
end if
if spot (7) > 0 then
if spot (7) = 1 then
Draw.Text ("X", 15, 115, font, brightred)
elsif spot (7) = 2 then
Draw.Text ("0", 15, 115, font, brightgreen)
end if
switchTurn
end if
if spot (8) > 0 then
if spot (8) = 1 then
Draw.Text ("X", 65, 115, font, brightred)
elsif spot (8) = 2 then
Draw.Text ("0", 65, 115, font, brightgreen)
end if
switchTurn
end if
if spot (9) > 0 then
if spot (9) = 1 then
Draw.Text ("X", 115, 115, font, brightred)
elsif spot (9) = 2 then
Draw.Text ("0", 115, 115, font, brightgreen)
end if
switchTurn
end if
exit when turn = choice
end if
end loop
end receive
procedure checkWin
if spot (1) = 1 and spot (2) = 1 and spot (3) = 1 then
win := 1
elsif spot (1) = 1 and spot (4) = 1 and spot (7) = 1 then
win := 1
elsif spot (1) = 1 and spot (5) = 1 and spot (9) = 1 then
win := 1
elsif spot (2) = 1 and spot (5) = 1 and spot (8) = 1 then
win := 1
elsif spot (3) = 1 and spot (5) = 1 and spot (7) = 1 then
win := 1
elsif spot (3) = 1 and spot (6) = 1 and spot (9) = 1 then
win := 1
elsif spot (4) = 1 and spot (5) = 1 and spot (6) = 1 then
win := 1
elsif spot (7) = 1 and spot (8) = 1 and spot (9) = 1 then
win := 1
elsif spot (1) = 2 and spot (2) = 2 and spot (3) = 2 then
win := 2
elsif spot (1) = 2 and spot (4) = 2 and spot (7) = 2 then
win := 2
elsif spot (1) = 2 and spot (5) = 2 and spot (9) = 2 then
win := 2
elsif spot (2) = 2 and spot (5) = 2 and spot (8) = 2 then
win := 2
elsif spot (3) = 2 and spot (5) = 2 and spot (7) = 2 then
win := 2
elsif spot (3) = 2 and spot (6) = 2 and spot (9) = 2 then
win := 2
elsif spot (4) = 2 and spot (5) = 2 and spot (6) = 2 then
win := 2
elsif spot (7) = 2 and spot (8) = 2 and spot (9) = 2 then
win := 2
end if
end checkWin
loop
put "Enter 1 to host a game."
put "Enter 2 to join a game."
get choice
exit when choice = 1 or choice = 2
end loop
if choice = 1 then
put "Game started on ", Net.LocalAddress
put "Waiting for connection..."
stream := Net.WaitForConnection (5505, netAddress)
else
put "Game address?"
get netAddress
stream := Net.OpenConnection (netAddress, 5505)
if stream <= 0 then
put "Failure connecting to Game."
return
else
put "Connected to ", netAddress
end if
end if
loop
delay (2000)
cls
setscreen ("graphics:170;190,nobuttonbar")
Draw.ThickLine (59, 11, 59, 159, 3, black)
Draw.ThickLine (109, 11, 109, 159, 3, black)
Draw.ThickLine (11, 59, 159, 59, 3, black)
Draw.ThickLine (11, 109, 159, 109, 3, black)
Draw.Box (10, 10, 160, 160, black)
loop
if turn = choice then
XorO
for i : 1 .. 9
put : stream, spot (i)
end for
checkWin
else
receive
checkWin
end if
if win > 0 then
exit
end if
end loop
if win = choice then
put "You win!"
else
put "You lose!"
end if
win := 0
if choice = 1 then
choice := 2
else
choice := 1
end if
for i : 1 .. 9
spot (i) := 0
end for
end loop |
sorry, but I don't have any comments because I don't really know what about half of the commands are doing, I just know how to use them
THNX
|
|
|
|
|
 |
Sponsor Sponsor

|
|
 |
Tony

|
Posted: Sun Apr 23, 2006 2:37 am Post subject: (No subject) |
|
|
maiku wrote: I don't really know what about half of the commands are doing, I just know how to use them
if you're interested in getting a better understanding on how something works and how to better use certain features, we have plenty of information in [Turing Tutorials]. Read up on the Turing Walkthrough to cover everything you need to know and to get ahead
|
Tony's programming blog. DWITE - a programming contest. |
|
|
|
 |
maiku

|
Posted: Sun Apr 23, 2006 10:26 am Post subject: (No subject) |
|
|
thnx!
you didnt happen to figure out what the prolem is, did you?
Ive been trying to figure it out for about a week.....it is angering me
at first, I thought it was screwing up because of my for statements, but then we actually learned arrays in class (im ahead of the class) and now i dont know why its not working, do i need to clear the variable values before getting new values using "get : stream,var"???
|
|
|
|
|
 |
Tony

|
Posted: Sun Apr 23, 2006 12:55 pm Post subject: (No subject) |
|
|
my guess would be that
code: |
if Net.CharAvailable (stream) then
for i : 1 .. 9
get : stream, spot (i)
end for
|
you check if a character is available, and then try to read 9 integers. Since reading from net buffer is faster than actually transfering all that data over, your program is likely trying to read something that is not yet there.
|
Tony's programming blog. DWITE - a programming contest. |
|
|
|
 |
maiku

|
Posted: Sun Apr 23, 2006 5:08 pm Post subject: (No subject) |
|
|
lol.....yeah i figured as much, but i dont know what Net command to use to read an integer. I really can't figure it out......
I'll try rewriting it using strings instead of integers, but its will take a LOT more lines
If you figure out what command, post it cuz im a noob
THNX!!!!!
|
|
|
|
|
 |
maiku

|
Posted: Fri Apr 28, 2006 11:00 am Post subject: Finished |
|
|
I got it working, thanks for the help!!! Here it is, if you ever find a need to use it.
code: | setscreen ("graphics:250;250,nobuttonbar,title:Tic-Tac-Toe")
var netAddress : string
var stream, choice : int
var x, y, b, turn, font, win : int
var Xwin, Owin, tie : int := 0
var spot : array 1 .. 9 of string
win := 0
turn := 1
font := Font.New ("Times New Roman:40")
for i : 1 .. 9
spot (i) := "empty"
end for
procedure switchTurn
if turn = 1 then
turn := 2
elsif turn = 2 then
turn := 1
end if
end switchTurn
procedure XorO
loop
Mouse.Where (x, y, b)
if b = 1 then
if x > 10 and x < 59 and y > 10 and y < 59 and spot (1) = "empty" then
if turn = 1 then
spot (1) := "x"
Draw.Text ("X", 15, 15, font, brightred)
elsif turn = 2 then
Draw.Text ("O", 15, 15, font, brightgreen)
spot (1) := "o"
end if
switchTurn
elsif x > 61 and x < 109 and y > 10 and y < 59 and spot (2) = "empty" then
if turn = 1 then
spot (2) := "x"
Draw.Text ("X", 65, 15, font, brightred)
elsif turn = 2 then
spot (2) := "o"
Draw.Text ("O", 65, 15, font, brightgreen)
end if
switchTurn
elsif x > 111 and x < 160 and y > 10 and y < 59 and spot (3) = "empty" then
if turn = 1 then
spot (3) := "x"
Draw.Text ("X", 115, 15, font, brightred)
elsif turn = 2 then
spot (3) := "o"
Draw.Text ("O", 115, 15, font, brightgreen)
end if
switchTurn
elsif x > 10 and x < 59 and y > 61 and y < 109 and spot (4) = "empty" then
if turn = 1 then
spot (4) := "x"
Draw.Text ("X", 15, 65, font, brightred)
elsif turn = 2 then
spot (4) := "o"
Draw.Text ("O", 15, 65, font, brightgreen)
end if
switchTurn
elsif x > 61 and x < 109 and y > 61 and y < 109 and spot (5) = "empty" then
if turn = 1 then
spot (5) := "x"
Draw.Text ("X", 65, 65, font, brightred)
elsif turn = 2 then
spot (5) := "o"
Draw.Text ("O", 65, 65, font, brightgreen)
end if
switchTurn
elsif x > 111 and x < 160 and y > 61 and y < 109 and spot (6) = "empty" then
if turn = 1 then
spot (6) := "x"
Draw.Text ("X", 115, 65, font, brightred)
elsif turn = 2 then
spot (6) := "o"
Draw.Text ("O", 115, 65, font, brightgreen)
end if
switchTurn
elsif x > 10 and x < 59 and y > 109 and y < 160 and spot (7) = "empty" then
if turn = 1 then
spot (7) := "x"
Draw.Text ("X", 15, 115, font, brightred)
elsif turn = 2 then
spot (7) := "o"
Draw.Text ("O", 15, 115, font, brightgreen)
end if
switchTurn
elsif x > 61 and x < 109 and y > 109 and y < 160 and spot (8) = "empty" then
if turn = 1 then
spot (8) := "x"
Draw.Text ("X", 65, 115, font, brightred)
elsif turn = 2 then
spot (8) := "o"
Draw.Text ("O", 65, 115, font, brightgreen)
end if
switchTurn
elsif x > 111 and x < 160 and y > 109 and y < 160 and spot (9) = "empty" then
if turn = 1 then
spot (9) := "x"
Draw.Text ("X", 115, 115, font, brightred)
elsif turn = 2 then
spot (9) := "o"
Draw.Text ("O", 115, 115, font, brightgreen)
end if
switchTurn
end if
end if
exit when turn not= choice
end loop
end XorO
procedure receive
loop
if Net.LineAvailable (stream) then
for i : 1 .. 9
get : stream, spot (i)
end for
if spot (1) not= "empty" then
if spot (1) = "x" then
Draw.Text ("X", 15, 15, font, brightred)
elsif spot (1) = "o" then
Draw.Text ("O", 15, 15, font, brightgreen)
end if
end if
if spot (2) not= "empty" then
if spot (2) = "x" then
Draw.Text ("X", 65, 15, font, brightred)
elsif spot (2) = "o" then
Draw.Text ("O", 65, 15, font, brightgreen)
end if
end if
if spot (3) not= "empty" then
if spot (3) = "x" then
Draw.Text ("X", 115, 15, font, brightred)
elsif spot (3) = "o" then
Draw.Text ("O", 115, 15, font, brightgreen)
end if
end if
if spot (4) not= "empty" then
if spot (4) = "x" then
Draw.Text ("X", 15, 65, font, brightred)
elsif spot (4) = "o" then
Draw.Text ("O", 15, 65, font, brightgreen)
end if
end if
if spot (5) not= "empty" then
if spot (5) = "x" then
Draw.Text ("X", 65, 65, font, brightred)
elsif spot (5) = "o" then
Draw.Text ("O", 65, 65, font, brightgreen)
end if
end if
if spot (6) not= "empty" then
if spot (6) = "x" then
Draw.Text ("X", 115, 65, font, brightred)
elsif spot (6) = "o" then
Draw.Text ("O", 115, 65, font, brightgreen)
end if
end if
if spot (7) not= "empty" then
if spot (7) = "x" then
Draw.Text ("X", 15, 115, font, brightred)
elsif spot (7) = "o" then
Draw.Text ("O", 15, 115, font, brightgreen)
end if
end if
if spot (8) not= "empty" then
if spot (8) = "x" then
Draw.Text ("X", 65, 115, font, brightred)
elsif spot (8) = "o" then
Draw.Text ("O", 65, 115, font, brightgreen)
end if
end if
if spot (9) not= "empty" then
if spot (9) = "x" then
Draw.Text ("X", 115, 115, font, brightred)
elsif spot (9) = "o" then
Draw.Text ("O", 115, 115, font, brightgreen)
end if
end if
switchTurn
exit when turn = choice
end if
end loop
end receive
procedure checkWin
if spot (1) = "x" and spot (2) = "x" and spot (3) = "x" then
win := 1
elsif spot (1) = "x" and spot (4) = "x" and spot (7) = "x" then
win := 1
elsif spot (1) = "x" and spot (5) = "x" and spot (9) = "x" then
win := 1
elsif spot (2) = "x" and spot (5) = "x" and spot (8) = "x" then
win := 1
elsif spot (3) = "x" and spot (5) = "x" and spot (7) = "x" then
win := 1
elsif spot (3) = "x" and spot (6) = "x" and spot (9) = "x" then
win := 1
elsif spot (4) = "x" and spot (5) = "x" and spot (6) = "x" then
win := 1
elsif spot (7) = "x" and spot (8) = "x" and spot (9) = "x" then
win := 1
elsif spot (1) = "o" and spot (2) = "o" and spot (3) = "o" then
win := 2
elsif spot (1) = "o" and spot (4) = "o" and spot (7) = "o" then
win := 2
elsif spot (1) = "o" and spot (5) = "o" and spot (9) = "o" then
win := 2
elsif spot (2) = "o" and spot (5) = "o" and spot (8) = "o" then
win := 2
elsif spot (3) = "o" and spot (5) = "o" and spot (7) = "o" then
win := 2
elsif spot (3) = "o" and spot (6) = "o" and spot (9) = "o" then
win := 2
elsif spot (4) = "o" and spot (5) = "o" and spot (6) = "o" then
win := 2
elsif spot (7) = "o" and spot (8) = "o" and spot (9) = "o" then
win := 2
end if
end checkWin
loop
put "Enter 1 to host a game."
put "Enter 2 to join a game."
get choice
exit when choice = 1 or choice = 2
end loop
if choice = 1 then
put "Game started on ", Net.LocalAddress
put "Waiting for connection..."
stream := Net.WaitForConnection (5505, netAddress)
else
put "Game address?"
get netAddress
stream := Net.OpenConnection (netAddress, 5505)
if stream <= 0 then
put "Failure connecting to Game."
return
else
put "Connected to ", netAddress
delay (2000)
end if
end if
loop
cls
setscreen ("graphics:170;200,nobuttonbar")
Draw.ThickLine (59, 11, 59, 159, 3, black)
Draw.ThickLine (109, 11, 109, 159, 3, black)
Draw.ThickLine (11, 59, 159, 59, 3, black)
Draw.ThickLine (11, 109, 159, 109, 3, black)
Draw.Box (10, 10, 160, 160, black)
if choice = 1 then
put "" : 9, Xwin, "-", Owin, "-", tie
else
put "" : 9, Owin, "-", Xwin, "-", tie
end if
loop
if turn = choice then
XorO
for i : 1 .. 9
put : stream, spot (i)
end for
checkWin
else
receive
checkWin
end if
if win > 0 then
exit
end if
if spot (1) not= "empty" and spot (2) not= "empty" and spot (3) not= "empty" and spot (4) not= "empty" and spot (5) not= "empty" and spot (6) not= "empty" and spot (7) not= "empty" and
spot (8) not= "empty" and spot (9) not= "empty" then
exit
end if
end loop
if win = choice then
put "" : 8, "You win!"
if choice = 1 then
Xwin += 1
else
Owin += 1
end if
elsif win = 0 then
put "" : 8, "Tie game!"
tie += 1
else
put "" : 8, "You lose!"
if choice = 1 then
Owin += 1
else
Xwin += 1
end if
end if
delay (2000)
win := 0
for i : 1 .. 9
spot (i) := "empty"
end for
end loop
%%%%%%%%%%%%%%%
% Tom Kempton %
%%%%%%%%%%%%%%%
|
I still haven't added any comments, but I think its pretty obvious how it works. (the thing at the end is my Tag)
|
|
|
|
|
 |
maiku

|
Posted: Sat May 20, 2006 11:28 pm Post subject: (No subject) |
|
|
i don't even know why i cared to put out the code, no one will ever use it
|
|
|
|
|
 |
Guest
|
Posted: Sat May 27, 2006 10:08 pm Post subject: (No subject) |
|
|
It's the first network game I've ever seen on these forums. I would use it, except for the fact at school, where everyone uses Turing at once, the Windows Firewall blocks network connections. I asked the teacher to ask the computer technician to allow Turing to access computers accross the network, but I don't think he will.
If it works without many bugs, it's probly the best game in my opinion on these forums, unless you can point me to a better net game.
|
|
|
|
|
 |
Sponsor Sponsor

|
|
 |
maiku

|
Posted: Sat May 27, 2006 10:38 pm Post subject: No bugs |
|
|
I can happily say "No bugs!"
And there can't be an input problem, because the only input is with the mouse
There may be a better network program out there, like the several chat programs that have been posted.
And what school do you go to? At my school, the firewall doesn't block turing..... me and my friend play Tic-Tac-Toe all the time
But I'm glad someone finally posted something
LOL
If you ever get it working on the network, have fun!
|
|
|
|
|
 |
Guest
|
Posted: Sat May 27, 2006 10:45 pm Post subject: (No subject) |
|
|
Well the school (KLCVI), only blocks Turing on the Windows XP computers, ironicly in my Programming class. Maybe the Firewall see's Turing as an unrequested script or something of that matter...
But if I wanted to play your game, I could use the downstairs computers in the Library, but I don't think my friend's would want to. I don't even like those computers. Stupid out of date windows ME pieces of crap.
|
|
|
|
|
 |
maiku

|
Posted: Sat May 27, 2006 11:12 pm Post subject: (No subject) |
|
|
I find that REALLY funny....
At my school, the XPs freeze whenever you try to run turing, and the old 98s or NT's run turing smooth as silk
But yeah, Tic-Tac-Toe gets boring really quickly
Although, try playing my blackjack game that i made, i have it posted here: http://www.compsci.ca/v2/viewtopic.php?t=12426&highlight=blackjack
Enjoy!
oh, and press any key to deal again, and run the bank balance reset program if you run out of money
|
|
|
|
|
 |
Clayton

|
Posted: Sun May 28, 2006 9:35 pm Post subject: (No subject) |
|
|
ME's,XP's i wish i had them, the best my school has are 98's running P2's, so yea, not a bad tictactoe game, however some of the code could have been more compartmentalized but whatever, good job
|
|
|
|
|
 |
maiku

|
Posted: Mon Nov 20, 2006 10:21 pm Post subject: DONE |
|
|
OK, I know its been a while but I finally got it how i want it to work.
Here it is. And if anyone has Hamachi, they're welcome to play against me if I can figure out how to work Hamachi. My network is Mootatta and the password is moo. message me here, on msn (this_email_is_fake@msn.com) or yahoo (guitarist_guy401@yahoo.com)
Hope to play against y'all.
|
|
|
|
|
 |
|
|