I am looking to find out exactly how to calculate a draw in the game tic tac toe
ID: 642071 • Letter: I
Question
I am looking to find out exactly how to calculate a draw in the game tic tac toe in F# I have found the winner but every scenario I can think of either keeps the board open or it closes upon first unempty spot it encounters:
Here is my code so far,
// Learn more about F# at http://fsharp.net
// See the 'F# Tutorial' project for more help.
open System
let gameBoard = [|[|" ";" ";" "|];
[|" ";" ";" "|];
[|" ";" ";" "|];|]
let mutable currentPlayer = "X"
let mutable winner = " "
let mutable cellAvailable = false
//let mutable turn = 0
let PrintBoard (board: string[][]) =
Console.WriteLine("-------------")
for row in 0..2 do
Console.Write("| ")
for col in 0..2 do
Console.Write(board.[row].[col] + " | ")
Console.WriteLine()
Console.WriteLine("-------------")
//checks for win
let CheckRows (board: string[][]) =
for row in 0 .. 2 do
if(board.[row].[0] = currentPlayer && board.[row].[1] = currentPlayer && board.[row].[2] = currentPlayer) then
winner <- currentPlayer
let CheckColumns (board: string[][]) =
for col in 0 .. 2 do
if(board.[0].[col] = currentPlayer && board.[1].[col] = currentPlayer && board.[2].[col] = currentPlayer) then
winner <- currentPlayer
let CheckDiagonals (board: string[][]) =
if(board.[0].[0] = currentPlayer && board.[1].[1] = currentPlayer && board.[2].[2] = currentPlayer) then
winner <- currentPlayer
elif(board.[0].[2] = currentPlayer && board.[1].[1] = currentPlayer && board.[2].[0] = currentPlayer) then
winner <- currentPlayer
let CheckWinner(board: string[][]) =
CheckRows board
CheckColumns board
CheckDiagonals board
if(winner <>" ") then Console.WriteLine("Congratulations "+winner+"! You have won the game")
//let CheckDraw (board: string[][])(row)(col) =
// if(board.[row].[col] = " ") then
let CheckAvailability (board: string[][])(row:int)(col:int) =
if(board.[row].[col] = " ") then cellAvailable <- true
else cellAvailable <- false
let ChangePlayer(board: string [][]) =
if(currentPlayer = "X") then currentPlayer <- "O"
else currentPlayer <- "X"
let ChangeBoard (board: string[][]) (player: string) (row:int) (col:int) =
board.[row].[col] <- player
[]
let main argv =
let mutable row = -1
let mutable col = -1
let mutable valid = 0
// let mutable turn = 0
PrintBoard gameBoard
while (winner = " ") do
Console.WriteLine("It is your turn Player " + currentPlayer)
while(valid = 0) do
Console.WriteLine("Please pick a valid row: 0 - 2")
row <- System.Int32.Parse(Console.ReadLine())
Console.WriteLine("Please pick a valid column: 0 - 2")
col <- System.Int32.Parse(Console.ReadLine())
if((row<0 || row>2 || col<0 || col>2)) then
Console.WriteLine("Invalid row/column. Please try again")
else
CheckAvailability (gameBoard) (row) (col)
if(cellAvailable = false) then
Console.WriteLine("The cell is not available. Please Try again!")
else
//change Board .. put x or o
ChangeBoard gameBoard currentPlayer row col
valid <- 1
// turn ++
valid <- 0
PrintBoard gameBoard
//check if there is a winner
CheckWinner gameBoard
ChangePlayer gameBoard
Console.ReadKey() |> ignore
0 // return an integer exit code
again any help will be useful thank you in advance!
Explanation / Answer
import java.awt.*; import java.awt.event.*; import java.applet.Applet; public class TicTacToe extends Applet implements MouseListener { Frame f; int flag=2,n,m,i=0; char ch[]=new char[9]; public TicTacToe() { f=new Frame("Tic Tac Toe"); f.setLayout(null); f.setVisible(true); f.setSize(600,600); f.addMouseListener(this); for(i=0;iRelated Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.