tic tac to assignment help
Author |
Message |
NovaK
|
Posted: Mon Feb 01, 2016 4:37 pm Post subject: tic tac to assignment help |
|
|
What is it you are trying to achieve?
<Replace all the <> with your answers/code and remove the <>>
What is the problem you are having?
<The program is suppose to play tic atc toe you against a computer the computers logic is flawed its suppose to cehck if it can defend a move such as blocking the human from winning example would be placing at spot 2 if 1 and 3 are full. then its suppose to play if it cant defend it is really broken because it does not play at all it willdo 1 move after the human move then it breaks it alo sometimes places over things>
Describe what you have tried to solve this problem
<Tried looking at the code im really stumped still pertty new with programming>
Post any relevant code (You may choose to attach the file instead of posting the code if it is too long)
<Answer Here>
Turing: |
<var played : array 1 .. 9 of int
var squares : array 1 .. 9 of int
var x : int
var win : int
var compmve : int
var pmove : int
var boardimg: int := Pic.FileNew ("board.gif")
var ximg: int := Pic.FileNew ("x.gif")
var oimg: int := Pic.FileNew ("o.gif")
squares (1) := 0
squares (2) := 0
squares (3) := 0
squares (4) := 0
squares (5) := 0
squares (6) := 0
squares (7) := 0
squares (8) := 0
squares (9) := 0
played (1) := 0
played (2) := 0
played (3) := 0
played (4) := 0
played (5) := 0
played (6) := 0
played (7) := 0
played (8) := 0
played (9) := 0
procedure draw (z : int, x : int, y : int)
Pic.Draw (z, x, y, picMerge)
end draw
procedure checkwin
if squares (1) = squares (2) & squares (2) = squares (3) then
win := 1
elsif
squares (4) = squares (5) & squares (5) = squares (6) then
win := 1
elsif squares (7) = squares (8) & squares (8) = squares (9) then
win := 1
elsif squares (1) = squares (4) & squares (4) = squares (7) then
win := 1
elsif squares (2) = squares (5) & squares (5) = squares (8) then
win := 1
elsif squares (3) = squares (6) & squares (6) = squares (9) then
win := 1
elsif squares (1) = squares (5) & squares (5) = squares (9) then
win := 1
elsif squares (3) = squares (5) & squares (5) = squares (7) then
win := 1
end if
end checkwin
win := 0
loop
loop
pmove := 0
put "Where would you like to place your X 1 to 9"
get x
squares (x ) := 1
played (x ) := 1
pmove := 1
exit when pmove = 1
cls
end loop
cls
draw (boardimg, 102, 105)
if squares (1) = 1 & played (1) = 1 then
draw (ximg, 100, 310)
end if
if squares (2) = 1 & played (2) = 1 then
draw (ximg, 205, 314)
end if
if squares (3)= 1 & played (3) = 1 then
draw (ximg, 310, 310)
end if
if squares (4) = 1 & played (4) = 1 then
draw (ximg, 101, 203)
end if
if squares (5) = 1 & played (5) = 1 then
draw (ximg, 205, 204)
end if
if squares (6) = 1 & played (6) = 1 then
draw (ximg, 304, 204)
end if
if squares (7) = 1 & played (7) = 1 then
draw (ximg, 100, 100)
end if
if squares (8) = 1 & played (8) = 1 then
draw (ximg, 204, 104)
end if
if squares (9) = 1 & played (9) = 1 then
draw (ximg, 304, 100)
end if
compmve := 0
loop
compmve := 0
if played (1) = 1 & played (3) = 1 then
squares (2) := 1
played (2) := 2
compmve := 1
exit when compmve = 1
end if
if played (4) = 1 & played (6) = 1 then
squares (5) := 1
played (5) := 2
compmve := 1
exit when compmve = 1
end if
if played (7) = 1 & played (9) = 1 then
squares (8) := 1
played (8) := 2
compmve := 1
exit when compmve = 1
end if
if played (1) = 1 & played (7) = 1 then
squares (4) := 1
played (4) := 2
compmve := 1
exit when compmve = 1
end if
if played (2) = 1 & played (8) = 1 then
squares (5) := 1
played (5) := 2
compmve := 1
exit when compmve = 1
end if
if played (3) = 1 & played (9) = 1 then
squares (6) := 1
played (6) := 2
compmve := 1
exit when compmve = 1
end if
if played (1) = 1 & played (9) = 1 then
squares (5) := 1
played (5) := 2
compmve := 1
exit when compmve = 1
end if
if played (3) = 1 & played (7) = 1 then
squares (5) := 1
played (5) := 2
compmve := 1
exit when compmve = 1
end if
if squares (1) = 0 & squares (3) = 0 then
squares (2) := 1
played (2) := 2
compmve := 1
exit when compmve = 1
end if
if squares (4) = 0 & squares (6) = 0 then
squares (5) := 1
played (5) := 2
compmve := 1
exit when compmve = 1
end if
if squares (7) = 0 & squares (9) = 0 then
squares (8) := 1
played (8) := 2
compmve := 1
exit when compmve = 1
end if
if squares (1) = 0 & squares (7) = 0 then
squares (4) := 1
played (4) := 2
compmve := 1
exit when compmve = 1
end if
if squares (2) = 0 & squares (8) = 0 then
squares (5) := 1
played (5) := 2
compmve := 1
exit when compmve = 1
end if
if squares (3) = 0 & squares (9) = 0 then
squares (6) := 1
played (6) := 2
compmve := 1
exit when compmve = 1
end if
if squares (1) = 0 & squares (9) = 0 then
squares (5) := 1
played (5) := 2
compmve := 1
exit when compmve = 1
end if
if squares (3) = 0 & squares (7) = 0 then
squares (5) := 1
played (5) := 2
compmve := 1
exit when compmve = 1
end if
if squares (1) = 0 & squares (4) = 0 then
squares (7) := 1
played (7) := 2
compmve := 1
exit when compmve = 1
end if
if squares (2) = 0 & squares (5) = 0 then
squares (8) := 1
played (8) := 2
compmve := 1
exit when compmve = 1
end if
if squares (3) = 0 & squares (6) = 0 then
squares (9) := 1
played (9) := 2
compmve := 1
exit when compmve = 1
end if
if squares (4) = 0 & squares (7) = 0 then
squares (1) := 1
played (1) := 2
compmve := 1
exit when compmve = 1
end if
if squares (5) = 0 & squares (8) = 0 then
squares (2) := 1
played (2) := 2
compmve := 1
exit when compmve = 1
end if
if squares (6) = 0 & squares (9) = 0 then
squares (3) := 1
played (3) := 2
exit when compmve = 1
end if
exit when compmve = 1
end loop
if squares (1) = 1 & played (1) = 2 then
draw (oimg, 100, 310)
end if
if squares (2) = 1 & played (2) = 2 then
draw (oimg, 205, 314)
end if
if squares (3)= 1 & played (3) = 2 then
draw (oimg, 310, 310)
end if
if squares (4) = 1 & played (4) = 2 then
draw (oimg, 101, 203)
end if
if squares (5) = 1 & played (5) = 2 then
draw (oimg, 205, 204)
end if
if squares (6) = 1 & played (6) = 2 then
draw (oimg, 304, 204)
end if
if squares (7) = 1 & played (7) = 2 then
draw (oimg, 100, 100)
end if
if squares (8) = 1 & played (8) = 2 then
draw (oimg, 204, 104)
end if
if squares (9) = 1 & played (9) = 2 then
draw (oimg, 304, 100)
end if
exit when win = 1
end loop>
|
Please specify what version of Turing you are using
<4.1.1>
http://s000.tinyupload.com/index.php?file_id=14942235473859021802 if u wanna dload it thx for the help fyi this is for a school course im only posting here because my teacher has not emailed me back its been 2 weeks |
|
|
|
|
|
Sponsor Sponsor
|
|
|
Insectoid
|
Posted: Mon Feb 01, 2016 6:06 pm Post subject: RE:tic tac to assignment help |
|
|
You've got a lot of code that does nothing here. Look at this loop. How many times will it execute?
code: | loop
pmove := 0
put "Where would you like to place your X 1 to 9"
get x
squares (x) := 1
played (x) := 1
pmove := 1
exit when pmove = 1
cls
end loop |
What is the played variable? Does it need to exist? Can you accomplish what you're trying to do with it without using it?
You have a lot of lines that look like this: code: |
compmve := 1
exit when compmve = 1 |
When will this ever return false? It won't. If a condition always returns true, then why have a condition at all?
Your AI checks if the player has played, for example, squares 1 and 2, and will select square 3 if he has. But if never checks if square 3 was already played, so on its next turn it will play that square again, so it looks like it doesn't do anything.
I strongly suggest you break up your big loop into a few procedures. A draw procedure, that draws the board and all of the x's and o's, a player turn procedure that processes the player's input and an AI procedure that controls the AI, so your main loop just looks like this:
code: | loop
drawBoard
playerMove
drawBoard
checkWin
AIMove
drawBoard
checkWin
end loop |
This will make it much, much easier to read and debug. |
|
|
|
|
|
NovaK
|
Posted: Wed Feb 03, 2016 3:00 pm Post subject: Re: tic tac to assignment help |
|
|
Thanks for your reply i am using the played variable to tell if it is the computer that filled that spot or human so i know who to say wins and im putting it all into procedures and 1 big loop thx for the advice |
|
|
|
|
|
Insectoid
|
Posted: Wed Feb 03, 2016 5:20 pm Post subject: RE:tic tac to assignment help |
|
|
Doesn't the variable squares store who filled that spot? |
|
|
|
|
|
|
|