// ----- GetRandomMove ------- (depth level 0) public static BoardSpace GetRandomMove(Board b, Player p) //Takes the board, and a player { List <BoardSpace> openSpaces = b.OpenSquares; Random r = new Random(); int randomIndex = r.Next(0, openSpaces.Count); BoardSpace newSpace = openSpaces[randomIndex]; bestSpace = newSpace; return((BoardSpace)bestSpace); }// ------End Random Move
//Choose initial square function If the AI is starting public void ChooseInitial() { if (depthLevel0) { if (b.OpenSquares.Count == b.BoardSize) { BoardSpace bs; Random r = new Random(); bs = new BoardSpace(r.Next(0, 3), r.Next(0, 3)); b[bs.X, bs.Y] = Player.O; DisplayBoard(); } } if (depthLevel1) { if (b.OpenSquares.Count == b.BoardSize) { BoardSpace bs; bs = DepthAI.Depth1Move(b, Player.O); b[bs.X, bs.Y] = Player.O; DisplayBoard(); } } if (depthLevel2) { if (b.OpenSquares.Count == b.BoardSize) { BoardSpace bs; bs = DepthAI.Depth2Move(b, Player.O); b[bs.X, bs.Y] = Player.O; DisplayBoard(); } } if (depthLevel3) { if (b.OpenSquares.Count == b.BoardSize) { BoardSpace bs; bs = DepthAI.Depth3Move(b, Player.O); b[bs.X, bs.Y] = Player.O; DisplayBoard(); } } }// End choose initial function
}// ------End Random Move // ------ Depth1move ------ (depth level 1) public static BoardSpace Depth1Move(Board b, Player p) //Takes the board, and a player { int count = 0; int maxIndex = 0; List <BoardSpace> openSpaces = b.OpenSquares; Board newBoard; newBoard = b.Clone(); for (int i = 0; i < openSpaces.Count; i++) { count = 0; BoardSpace newSpace = openSpaces[i]; newBoard[newSpace.X, newSpace.Y] = p; count += newBoard.possibilityToWin; ColPosToWin.Add(count); exploredSpaces.Add(newSpace); if (i != openSpaces.Count - 1) { newBoard = b.Clone(); } else { maxIndex = ColPosToWin.IndexOf(ColPosToWin.Max()); finalSpace = exploredSpaces[maxIndex]; count = 0; maxIndex = 0; exploredSpaces.Clear(); ColPosToWin.Clear(); bestSpace = finalSpace; } } return((BoardSpace)bestSpace); }// End Depth1Move
// ------ Depth2move ------ (depth level 2) public static BoardSpace Depth2Move(Board b, Player p) //Takes the board, and a player { int count = 0; int countSeveralBoards = 0; int maxIndex = 0; List <BoardSpace> openSpaces = b.OpenSquares; Board newBoard; newBoard = b.Clone(); for (int i = 0; i < openSpaces.Count; i++) { newBoard = b.Clone(); count = 0; countSeveralBoards = 0; BoardSpace newSpace = openSpaces[i]; newBoard[newSpace.X, newSpace.Y] = p; exploredSpaces.Add(newSpace); List <BoardSpace> tempOpenSpaces = newBoard.OpenSquares; tempBoard = newBoard.Clone(); if (!newBoard.IsComplete) { for (int j = 0; j < tempOpenSpaces.Count; j++) { count = 0; BoardSpace tempNewSpace = tempOpenSpaces[j]; tempBoard[tempNewSpace.X, tempNewSpace.Y] = Player.X; count += tempBoard.possibilityToWin; countSeveralBoards += count; tempBoard = newBoard.Clone(); if (j == tempOpenSpaces.Count - 1) { ColPosToWin.Add(countSeveralBoards); } } } else { ColPosToWin.Add(5); } if (i == openSpaces.Count - 1) { maxIndex = ColPosToWin.IndexOf(ColPosToWin.Max()); finalSpace = exploredSpaces[maxIndex]; count = 0; maxIndex = 0; exploredSpaces.Clear(); ColPosToWin.Clear(); bestSpace = finalSpace; } } return((BoardSpace)bestSpace); }// End Depth2Move
}// End choose initial function // Eventhandlers by clicking private void BtnClick(object sender, EventArgs e) { //If The player starts if (playerStart) { BoardSpace bs = (BoardSpace)sender; //creating new boardspace as object sender b[bs.X, bs.Y] = Player.X; //First turn is the player DisplayBoard(); //Display board after choosing if (WinnerCheck()) //Check if win, if so, then reload { Form1_Load(null, new EventArgs()); } if (depthLevel0) { if (b.OpenSquares.Count != b.BoardSize) { bs = DepthAI.GetRandomMove(b, Player.O); b[bs.X, bs.Y] = Player.O; DisplayBoard(); if (WinnerCheck()) { Form1_Load(null, new EventArgs()); } } } if (depthLevel1) { if (b.OpenSquares.Count != b.BoardSize) { bs = DepthAI.Depth1Move(b, Player.O); b[bs.X, bs.Y] = Player.O; DisplayBoard(); if (WinnerCheck()) { Form1_Load(null, new EventArgs()); } } } if (depthLevel2) { if (b.OpenSquares.Count != b.BoardSize) { bs = DepthAI.Depth2Move(b, Player.O); b[bs.X, bs.Y] = Player.O; DisplayBoard(); if (WinnerCheck()) { Form1_Load(null, new EventArgs()); } } } if (depthLevel3) { if (b.OpenSquares.Count != b.BoardSize) { bs = DepthAI.Depth3Move(b, Player.O); b[bs.X, bs.Y] = Player.O; DisplayBoard(); if (WinnerCheck()) { Form1_Load(null, new EventArgs()); } } } }// End playerStart //If the AI starts if (AIStart) { BoardSpace bs = (BoardSpace)sender; //creating new boardspace as object sender if (b.OSquares.Count == 0) //AI starts (if there is no opponents on the board choose initial { ChooseInitial(); } else { //Player turn b[bs.X, bs.Y] = Player.X; //First turn is the player DisplayBoard(); //Display board after choosing if (WinnerCheck()) //Check if win, if so, then reload { Form1_Load(null, new EventArgs()); } //AI turn if (depthLevel0) { bs = DepthAI.GetRandomMove(b, Player.O); b[bs.X, bs.Y] = Player.O; DisplayBoard(); if (WinnerCheck()) { Form1_Load(null, new EventArgs()); ChooseInitial(); // If The AI wins, select random square } } if (depthLevel1) //AI turn { bs = DepthAI.Depth1Move(b, Player.O); b[bs.X, bs.Y] = Player.O; DisplayBoard(); if (WinnerCheck()) { Form1_Load(null, new EventArgs()); ChooseInitial(); // If The AI wins, select random square } } if (depthLevel2) //AI turn { bs = DepthAI.Depth2Move(b, Player.O); b[bs.X, bs.Y] = Player.O; DisplayBoard(); if (WinnerCheck()) { Form1_Load(null, new EventArgs()); ChooseInitial(); // If The AI wins, select random square } } if (depthLevel3) //AI turn { bs = DepthAI.Depth3Move(b, Player.O); b[bs.X, bs.Y] = Player.O; DisplayBoard(); if (WinnerCheck()) { Form1_Load(null, new EventArgs()); ChooseInitial(); // If The AI wins, select random square } } } } // End AIStart } // End button click