##tac tac toe game for maplev4 #Human plays first tictactoe:=proc() local i,place1,turn,ans; global s1,s2,s3,s4,s5,s6,s7,s8,s9,stoop; for i from 1 to 9 do s.i:=` `; od; print(`Tic-tac-toe game for 2 players`); print(` `); print(`Do you want to play tic-tac-toe? (Enter "Y" or "N" - no semicolon)`); ans := readline(terminal); #print(`ans=`,ans,evalb(ans = ('Y') or ans = y)); if not(evalb(ans = ('Y') or ans = y)) then print(`Thanks for playing tic-tac-toe.`); RETURN(); fi; print(`You play "x" and the computer plays "o"`); print(`Make your first move by entering a number`); print(`1 through 9 as in the array below:`); print(array(1..3,1..3,[[1,2,3],[4,5,6],[7,8,9]])); print(` `); for turn from 1 to 15 do if evalb(isdraw()=`y`) then print(`Drawn game - thanks for playing tic-tac-toe with me`); break; RETURN(); fi; print(`Where do you want to play? (Enter "0", ..., "9" - "0" stops)`); place.turn := readline(terminal); ans:=parse(place.turn); #print(turn,place.turn,ans,s.ans,whattype(place.turn),whattype(ans)); if evalb(ans=0) then print(`Thanks for playing tic-tac-toe.`); RETURN(); fi; if (evalb(s.ans='o') or evalb(s.ans='x')) then print(`Try again at another place.`); next; else s.ans:='x'; #print(`s.ans = `,s.ans); fi; print(`Your move:`); print(array(1..3,1..3,[[s.1,s.2,s.3],[s.4,s.5,s.6],[s.7,s.8,s.9]])); nextmove(); if stoop=`Y` then # print(`Thanks for playing tic-tac-toe.`); RETURN(); fi; od; ##Lots more cases end: nextmove:=proc() local i,j,s; global stoop; if ((s.1=`x` and s.5=`x` and s.9=`x`) or (s.1=`x` and s.2=`x` and s.3=`x`) or (s.1=`x` and s.4=`x` and s.7=`x`) or (s.2=`x` and s.5=`x` and s.8=`x`) or (s.3=`x` and s.5=`x` and s.7=`x`) or (s.3=`x` and s.6=`x` and s.9=`x`) or (s.7=`x` and s.8=`x` and s.9=`x`) or (s.4=`x` and s.5=`x` and s.6=`x`)) then print(`You win. Thanks for playing tic-tac-toe.`); stoop:=`Y`; RETURN(stoop): fi; if ((s.1=`o` and s.5=`o` and s.9=`o`) or (s.1=`o` and s.2=`o` and s.3=`o`) or (s.1=`o` and s.4=`o` and s.7=`o`) or (s.2=`o` and s.5=`o` and s.8=`o`) or (s.3=`o` and s.5=`o` and s.7=`o`) or (s.3=`o` and s.6=`o` and s.9=`o`) or (s.7=`o` and s.8=`o` and s.9=`o`) or (s.4=`o` and s.5=`o` and s.6=`o`)) then print(`Computer wins. Thanks for playing tic-tac-toe.`); RETURN(): fi; ##Computer wins in 1 move ###1-5-9 if (s.1=`o` and s.5=`o` and s.9=` `) then s.9:=`o`; printmove(); RETURN(): fi; if (s.1=`o` and s.9=`o` and s.5=` `) then s.5:=`o`; printmove(); RETURN(): fi; if (s.5=`o` and s.9=`o` and s.1=` `) then s.1:=`o`; printmove(); RETURN(): fi; ###1-2-3 if (s.1=`o` and s.2=`o` and s.3=` `) then s.3:=`o`; printmove(); RETURN(): fi; if (s.1=`o` and s.3=`o` and s.2=` `) then s.2:=`o`; printmove(); RETURN(): fi; if (s.2=`o` and s.3=`o` and s.1=` `) then s.1:=`o`; printmove(); RETURN(): fi; ###1-4-7 if (s.1=`o` and s.4=`o` and s.7=` `) then s.7:=`o`; printmove(); RETURN(): fi; if (s.1=`o` and s.7=`o` and s.4=` `) then s.4:=`o`; printmove(); RETURN(): fi; if (s.7=`o` and s.4=`o` and s.1=` `) then s.1:=`o`; printmove(); RETURN(): fi; ##Lots more cases ###1-5-9 if (s.1=`x` and s.5=`x` and s.9=` `) then s.9:=`o`; printmove(); RETURN(): fi; if (s.1=`x` and s.9=`x` and s.5=` `) then s.5:=`o`; printmove(); RETURN(): fi; if (s.9=`x` and s.5=`x` and s.1=` `) then s.1:=`o`; printmove(); RETURN(): fi; ###1-2-3 if (s.1=`x` and s.2=`x` and s.3=` `) then s.3:=`o`; printmove(); RETURN(): fi; if (s.1=`x` and s.3=`x` and s.2=` `) then s.2:=`o`; printmove(); RETURN(): fi; if (s.3=`x` and s.2=`x` and s.1=` `) then s.1:=`o`; printmove(); RETURN(): fi; ###1-4-7 if (s.1=`x` and s.4=`x` and s.7=` `) then s.7:=`o`; printmove(); RETURN(): fi; if (s.1=`x` and s.7=`x` and s.4=` `) then s.4:=`o`; printmove(); RETURN(): fi; if (s.4=`x` and s.7=`x` and s.1=` `) then s.1:=`o`; printmove(); RETURN(): fi; ###4-5-6 if (s.4=`x` and s.5=`x` and s.6=` `) then s.6:=`o`; printmove(); RETURN(): fi; if (s.4=`x` and s.6=`x` and s.5=` `) then s.5:=`o`; printmove(); RETURN(): fi; if (s.6=`x` and s.5=`x` and s.4=` `) then s.4:=`o`; printmove(); RETURN(): fi; ###7-8-9 if (s.7=`x` and s.8=`x` and s.9=` `) then s.9:=`o`; printmove(); RETURN(): fi; if (s.7=`x` and s.9=`x` and s.8=` `) then s.8:=`o`; printmove(); RETURN(): fi; if (s.9=`x` and s.8=`x` and s.7=` `) then s.7:=`o`; printmove(); RETURN(): fi; ### Lots more cases ##Make a "random" move: if s.5=` ` then s.5:=`o`; printmove(); RETURN(): fi; for i from 1 to 9 do if evalb(s.i=` `) then s.i:=`o`; printmove(); break; RETURN(): fi; od; ##Lots more cases if isdraw=`y` then print(`Drawn game - thanks for playing tic-tac-toe with me`); stoop:=`Y`; RETURN(); fi; #RETURN([s.1,s.2,s.3,s.4,s.5,s.6,s.7,s.8,s.9]); end: printmove:=proc() global stoop; print(`Computer's move:`); print(array(1..3,1..3,[[s.1,s.2,s.3],[s.4,s.5,s.6],[s.7,s.8,s.9]])); # print(isdraw()); if evalb(isdraw()=`y`) then print(`Drawn game - thanks for playing tic-tac-toe with me`); stoop:=`Y`; RETURN(); fi; print(`Your turn.`); RETURN(): end: isdraw:=proc() local i,draw; draw:=`y`; for i from 1 to 9 do if evalb(s.i=` `) then draw:=`n`; fi; od; RETURN(draw); end: