//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 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