a. for an easier version, let the computer make its move in any available square
(i did this part not to do with this question)
b. For a more challenging program, have the computer find its best move.
now straight away i started looking on internet and found minimax thoery with alpha-beta but whether i just cant understand the online tutorials examples or seeing things i havent learnt yet only makes me think i will learn the parts im missing later in the book?
here are my problems,
i managed to get the game to check if user or player can win in next move and stop this but maybe due to way i have wrote the loops i cannot see a way of prioritizing going for the win over stopping user winning for example
X | | X
. | X |
O | | O
due to the checkwin order it will pick up the X(user) defend rather than take the O(AI) win?
also when either isnt going to win next move , i have it so the computer generates random number then sees if its available if so fill space in but i guess this is where minimax would tell it the "best" position to go?
here is my code
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Drawing; using System.Drawing.Drawing2D; using System.Windows.Forms; public class TicTacToe : Form { int maxturn; int[] squareempty = new int[9]; int index = 10; int endofgame = 0; int whosturn; // 0 for comp 1 for user int[] wongame = new int[9]; // button for new game // create the boxes ///////////////////////////////////// //resetbutton GraphicsPath reset = new GraphicsPath(); Region resetregion = new Region(); /// ///////////////////////////////// // array rectangles //RectangleF[][] rectanglepoints = new RectangleF[9][]; GraphicsPath[] grid = new GraphicsPath[9]; Region[] gridregion = new Region[9]; //////////////////////////////////////// //String X props GraphicsPath[] xicons = new GraphicsPath[9]; Region[] xiconregion = new Region[9]; /// <summary> /// array for x and o values /// </summary> int[] XOvalues = new int[9]; // array for checked boxes int[] checkedboxes = new int[9]; Random Turn = new Random(); Random r = new Random(); int[] oldX = new int[9]; int[] oldY = new int[9]; bool check1 = false; bool check2 = false; bool check3 = false; bool check4 = false; bool check5 = false; bool check6 = false; bool check7 = false; bool check8 = false; bool check9 = false; bool check10 = false; bool check11 = false; bool check12 = false; bool check13 = false; bool check14 = false; bool check15 = false; bool check16 = false; bool check17 = false; bool check18 = false; bool check19 = false; bool check20 = false; bool check21 = false; bool check22 = false; bool check23 = false; bool check24 = false; bool firstturn = true; public TicTacToe() { Size = new Size(800, 800); Text = "Tic Tac Toe"; BackColor = Color.Red; int w = DisplayRectangle.Width - 20; int h = DisplayRectangle.Height - 20; whosturn = Turn.Next(2); if (whosturn == 1) { firstturn = false; } ////////////////////////// // reset button Point resetpoint = new Point(w / 10, h / 10 * 9); reset.AddString("Reset", FontFamily.GenericSerif, 0, 60, resetpoint, StringFormat.GenericDefault); resetregion = new Region(reset); ////////////////////////// // Array of rectangles // 100, h / 50, 150, 50 } public void Initboard(Graphics g) { int w = DisplayRectangle.Width; int h = DisplayRectangle.Height; int hofrect = h / 20 * 5; int sizeofsquare = w / 3; RectangleF[] rectanglepoints = { new RectangleF(w / 20+5, h / 20+5, w / 20 * 6 - 10, h/20 * 5-10), new RectangleF(w / 20 * 7, h / 20+5, w / 20 * 6 - 5, h / 20 * 5-10), new RectangleF(w / 20*13, h / 20+5, w / 20 * 6-5, h / 20 * 5-10), new RectangleF(w / 20+5, h / 20*6, w / 20 * 6 - 10, h/20*5-5), new RectangleF(w / 20 * 7, h / 20*6, w / 20 * 6 - 5, h / 20 * 5-5), new RectangleF(w / 20 * 13, h / 20*6, w / 20 * 6-5, h / 20 * 5-5), new RectangleF(w / 20+5, h / 20*11, w / 20 * 6 - 10, h/20*5-5), new RectangleF(w / 20 * 7, h/20*11, w / 20 * 6 - 5, h / 20 * 5-5), new RectangleF(w / 20 * 13, h/20*11, w / 20 * 6-5, h / 20 * 5-5) }; for (int i = 0; i < rectanglepoints.Length; i++) { grid[i] = new GraphicsPath(); grid[i].AddRectangle(rectanglepoints[i]); gridregion[i] = new Region(grid[i]); } Pen outline = new Pen(Color.White, 5); //reset button g.FillRegion(Brushes.White, resetregion); g.DrawRectangle(outline, w / 10, h / 10 * 9 - 20, w / 10 * 2, h / 10 + 10); g.DrawRectangle(Pens.Black, w / 10, h / 10 * 9 - 20, w / 10 * 2, h / 10 + 10); g.DrawRectangle(Pens.Black, w / 10 - 3, h / 10 * 9 - 23, w / 10 * 2 + 5, h / 10 + 15); g.DrawRectangle(Pens.Black, w / 10 + 3, h / 10 * 9 - 17, w / 10 * 2 - 5, h / 10 + 5); //////////////////////////////////////////////// // Xicons RectangleF[] xiconpoints = { new RectangleF(w / 20+5, h / 20+5, w / 20 * 6 - 10, h/20 * 5-10), new RectangleF(w / 20 * 7, h / 20+5, w / 20 * 6 - 5, h / 20 * 5-10), new RectangleF(w / 20*13, h / 20+5, w / 20 * 6-5, h / 20 * 5-10), new RectangleF(w / 20+5, h / 20*6, w / 20 * 6 - 10, h/20*5-5), new RectangleF(w / 20 * 7, h / 20*6, w / 20 * 6 - 5, h / 20 * 5-5), new RectangleF(w / 20 * 13, h / 20*6, w / 20 * 6-5, h / 20 * 5-5), new RectangleF(w / 20+5, h / 20*11, w / 20 * 6 - 10, h/20*5-5), new RectangleF(w / 20 * 7, h/20*11, w / 20 * 6 - 5, h / 20 * 5-5), new RectangleF(w / 20 * 13, h/20*11, w / 20 * 6-5, h / 20 * 5-5) }; PointF newpoint = new PointF(100, 100); for (int x = 0; x < 1; x++) { xicons[x] = new GraphicsPath(); xicons[x].AddString("X", FontFamily.GenericSerif, 1, 100, newpoint, StringFormat.GenericDefault); xiconregion[x] = new Region(xicons[x]); } //white background for game g.FillRectangle(Brushes.White, w / 20, h / 20, w / 20 * 18, h / 20 * 15); // grid outlines Pen Blackline = new Pen(Brushes.Black, 5); g.DrawLine(Blackline, w / 20 - 1, h / 20 + 2, w / 20 * 19, h / 20 + 2); g.DrawLine(Blackline, w / 20 - 1, h / 20 * 16 - 3, w / 20 * 19, h / 20 * 16 - 3); g.DrawLine(Blackline, w / 20 * 19 - 3, h / 20, w / 20 * 19 - 3, h / 20 * 16 - 3); g.DrawLine(Blackline, w / 20 + 2, h / 20 * 16 - 3, w / 20 + 2, h / 20); g.DrawLine(Blackline, w / 20 * 7 - 3, h / 20 * 16 - 3, w / 20 * 7 - 3, h / 20); g.DrawLine(Blackline, w / 20 * 13 - 3, h / 20 * 16 - 3, w / 20 * 13 - 3, h / 20); g.DrawLine(Blackline, w / 20 - 1, h / 20 * 6 - 3, w / 20 * 19, h / 20 * 6 - 3); g.DrawLine(Blackline, w / 20 - 1, h / 20 * 11 - 3, w / 20 * 19, h / 20 * 11 - 3); } protected override void OnPaint(PaintEventArgs e) { Graphics g = e.Graphics; Initboard(g); CheckForWin(g); ArrayScores(g); AiDefendorWinMove(); AiBestMove(); base.OnPaint(e); } public void AiDefendorWinMove() { if (endofgame == 0 && whosturn == 0) { //while (whosturn == 0) //{ if (whosturn == 0 && firstturn == true) { int firstgo = r.Next(9); index = firstgo; XOvalues[index] = 5; checkedboxes[index] = index; wongame[index] = 5; whosturn = 1; firstturn = false; maxturn++; Invalidate(); } //check to stop player winning , loops below checks first two columns if (XOvalues[0] == XOvalues[1] && XOvalues[2] == 0 && check1 == false && whosturn == 0 && XOvalues[0] != 0) { index = 2; XOvalues[index] = 5; checkedboxes[index] = index; wongame[index] = 5; whosturn = 1; check1 = true; maxturn++; Invalidate(); } if (XOvalues[3] == XOvalues[4] && XOvalues[5] == 0 && check2 == false && whosturn == 0 && XOvalues[3] != 0) { index = 5; XOvalues[index] = 5; checkedboxes[index] = index; wongame[index] = 5; whosturn = 1; check2 = true; maxturn++; Invalidate(); } if (XOvalues[6] == XOvalues[7] && XOvalues[8] == 0 && check3 == false && whosturn == 0 && XOvalues[6] != 0) { index = 8; XOvalues[index] = 5; checkedboxes[index] = index; wongame[index] = 5; whosturn = 1; check3 = true; maxturn++; Invalidate(); } //check to stop player winning , loops below checks last two columns if (XOvalues[1] == XOvalues[2] && XOvalues[0] == 0 && check4 == false && whosturn == 0 && XOvalues[1] != 0) { index = 0; XOvalues[index] = 5; checkedboxes[index] = index; wongame[index] = 5; whosturn = 1; check4 = true; maxturn++; Invalidate(); } if (XOvalues[4] == XOvalues[5] && XOvalues[3] == 0 && check5 == false && whosturn == 0 && XOvalues[4] != 0) { index = 3; XOvalues[index] = 5; checkedboxes[index] = index; wongame[index] = 5; whosturn = 1; check5 = true; maxturn++; Invalidate(); } if (XOvalues[7] == XOvalues[8] && XOvalues[6] == 0 && check6 == false && whosturn == 0 && XOvalues[7] != 0) { index = 6; XOvalues[index] = 5; checkedboxes[index] = index; wongame[index] = 5; whosturn = 1; check6 = true; maxturn++; Invalidate(); } //check to stop player winning , loops below checks first and last columns if (XOvalues[0] == XOvalues[2] && XOvalues[1] == 0 && check7 == false && whosturn == 0 && XOvalues[0] != 0) { index = 1; XOvalues[index] = 5; checkedboxes[index] = index; wongame[index] = 5; whosturn = 1; check7 = true; maxturn++; Invalidate(); } if (XOvalues[3] == XOvalues[5] && XOvalues[4] == 0 && check8 == false && whosturn == 0 && XOvalues[3] != 0) { index = 4; XOvalues[index] = 5; checkedboxes[index] = index; wongame[index] = 5; whosturn = 1; check8 = true; maxturn++; Invalidate(); } if (XOvalues[6] == XOvalues[8] && XOvalues[7] == 0 && check9 == false && whosturn == 0 && XOvalues[6] != 0) { index = 7; XOvalues[index] = 5; checkedboxes[index] = index; wongame[index] = 5; whosturn = 1; check9 = true; maxturn++; Invalidate(); } //check to stop player winning , loops below checks first 2 rows if (XOvalues[0] == XOvalues[3] && XOvalues[6] == 0 && check10 == false && whosturn == 0 && XOvalues[0] != 0) { index = 6; XOvalues[index] = 5; checkedboxes[index] = index; wongame[index] = 5; whosturn = 1; check10 = true; maxturn++; Invalidate(); } if (XOvalues[1] == XOvalues[4] && XOvalues[7] == 0 && check11 == false && whosturn == 0 && XOvalues[1] != 0) { index = 7; XOvalues[index] = 5; checkedboxes[index] = index; wongame[index] = 5; whosturn = 1; check11 = true; maxturn++; Invalidate(); } if (XOvalues[2] == XOvalues[5] && XOvalues[8] == 0 && check12 == false && whosturn == 0 && XOvalues[2] != 0) { index = 8; XOvalues[index] = 5; checkedboxes[index] = index; wongame[index] = 5; whosturn = 1; check12 = true; maxturn++; Invalidate(); } //check to stop player winning , loops below checks last 2 rows if (XOvalues[3] == XOvalues[6] && XOvalues[0] == 0 && check13 == false && whosturn == 0 && XOvalues[3] != 0) { index = 0; XOvalues[index] = 5; checkedboxes[index] = index; wongame[index] = 5; whosturn = 1; check13 = true; maxturn++; Invalidate(); } if (XOvalues[4] == XOvalues[7] && XOvalues[1] == 0 && check14 == false && whosturn == 0 && XOvalues[4] != 0) { index = 1; XOvalues[index] = 5; checkedboxes[index] = index; wongame[index] = 5; whosturn = 1; check14 = true; maxturn++; Invalidate(); } if (XOvalues[5] == XOvalues[8] && XOvalues[2] == 0 && check15 == false && whosturn == 0 && XOvalues[5] != 0) { index = 2; XOvalues[index] = 5; checkedboxes[index] = index; wongame[index] = 5; whosturn = 1; check15 = true; maxturn++; Invalidate(); } //check to stop player winning , loops below checks first and last rows if (XOvalues[0] == XOvalues[6] && XOvalues[3] == 0 && check16 == false && whosturn == 0 && XOvalues[0] != 0) { index = 3; XOvalues[index] = 5; checkedboxes[index] = index; wongame[index] = 5; whosturn = 1; check16 = true; maxturn++; Invalidate(); } if (XOvalues[1] == XOvalues[7] && XOvalues[4] == 0 && check17 == false && whosturn == 0 && XOvalues[1] != 0) { index = 4; XOvalues[index] = 5; checkedboxes[index] = index; wongame[index] = 5; whosturn = 1; check17 = true; maxturn++; Invalidate(); } if (XOvalues[2] == XOvalues[8] && XOvalues[5] == 0 && check18 == false && whosturn == 0 && XOvalues[2] != 0) { index = 5; XOvalues[index] = 5; checkedboxes[index] = index; wongame[index] = 5; whosturn = 1; check18 = true; maxturn++; Invalidate(); } //check to stop player winning , loops below checks diagonal wins if (XOvalues[0] == XOvalues[4] && XOvalues[8] == 0 && check19 == false && whosturn == 0 && XOvalues[0] != 0) { index = 8; XOvalues[index] = 5; checkedboxes[index] = index; wongame[index] = 5; whosturn = 1; check19 = true; maxturn++; Invalidate(); } if (XOvalues[4] == XOvalues[8] && XOvalues[0] == 0 && check20 == false && whosturn == 0 && XOvalues[4] != 0) { index = 0; XOvalues[index] = 5; checkedboxes[index] = index; wongame[index] = 5; whosturn = 1; check20 = true; maxturn++; Invalidate(); } if (XOvalues[0] == XOvalues[8] && XOvalues[4] == 0 && check21 == false && whosturn == 0 && XOvalues[0] != 0) { index = 4; XOvalues[index] = 5; checkedboxes[index] = index; wongame[index] = 5; whosturn = 1; check21 = true; maxturn++; Invalidate(); } if (XOvalues[2] == XOvalues[4] && XOvalues[6] == 0 && check22 == false && whosturn == 0 && XOvalues[2] != 0) { index = 6; XOvalues[index] = 5; checkedboxes[index] = index; wongame[index] = 5; whosturn = 1; check22 = true; maxturn++; Invalidate(); } if (XOvalues[6] == XOvalues[4] && XOvalues[2] == 0 && check23 == false && whosturn == 0 && XOvalues[6] != 0) { index = 2; XOvalues[index] = 5; checkedboxes[index] = index; wongame[index] = 5; whosturn = 1; check23 = true; maxturn++; Invalidate(); } if (XOvalues[2] == XOvalues[6] && XOvalues[4] == 0 && check24 == false && whosturn == 0 && XOvalues[6] != 0) { index = 4; XOvalues[index] = 5; checkedboxes[index] = index; wongame[index] = 5; whosturn = 1; check24 = true; maxturn++; Invalidate(); } // whosturn = 1; // maxturn++; // } } } public void AiBestMove() { if (endofgame == 0) { if (whosturn == 0) { int Bestmove = r.Next(9); for (int i = 0; i < 100; i++) { if (XOvalues[Bestmove] == 0) { index = Bestmove; XOvalues[index] = 5; checkedboxes[index] = index; wongame[index] = 5; whosturn = 1; maxturn++; Invalidate(); break; } else { Bestmove = r.Next(9); } } } } } public void CheckForWin(Graphics g) { int w = DisplayRectangle.Width; int h = DisplayRectangle.Height; // See if player won String Userwins = "User Wins!"; String CompWins = "Computer Wins!"; Font winfont = new Font("Arial", w / 20, FontStyle.Regular); int strCompWin = (int)g.MeasureString(CompWins,winfont).Width; int StrCompWinPlace = w - strCompWin / 2; Font wincompfont = new Font("Arial", 30, FontStyle.Regular); int win1 = (wongame[0] + wongame[1] + wongame[2]); int win2 = (wongame[3] + wongame[4] + wongame[5]); int win3 = (wongame[6] + wongame[7] + wongame[8]); int win4 = (wongame[0] + wongame[3] + wongame[6]); int win5 = (wongame[1] + wongame[4] + wongame[7]); int win6 = (wongame[2] + wongame[5] + wongame[8]); int win7 = (wongame[0] + wongame[4] + wongame[8]); int win8 = (wongame[2] + wongame[4] + wongame[6]); if (win1 == 6) { g.DrawString(Userwins, winfont, Brushes.Black, w / 20 * 10, h / 20 * 17); g.FillRegion(Brushes.Red, gridregion[0]); g.FillRegion(Brushes.Red, gridregion[1]); g.FillRegion(Brushes.Red, gridregion[2]); endofgame = 1; } if (win2 == 6) { g.DrawString(Userwins, winfont, Brushes.Black, w / 20 * 10, h / 20 * 17); g.FillRegion(Brushes.Red, gridregion[3]); g.FillRegion(Brushes.Red, gridregion[4]); g.FillRegion(Brushes.Red, gridregion[5]); endofgame = 1; } if (win3 == 6) { g.DrawString(Userwins, winfont, Brushes.Black, w / 20 * 10, h / 20 * 17); g.FillRegion(Brushes.Red, gridregion[6]); g.FillRegion(Brushes.Red, gridregion[7]); g.FillRegion(Brushes.Red, gridregion[8]); endofgame = 1; } if (win4 == 6) { g.DrawString(Userwins, winfont, Brushes.Black, w / 20 * 10, h / 20 * 17); g.FillRegion(Brushes.Red, gridregion[0]); g.FillRegion(Brushes.Red, gridregion[3]); g.FillRegion(Brushes.Red, gridregion[6]); endofgame = 1; } if (win5 == 6) { g.DrawString(Userwins, winfont, Brushes.Black, w / 20 * 10, h / 20 * 17); g.FillRegion(Brushes.Red, gridregion[1]); g.FillRegion(Brushes.Red, gridregion[4]); g.FillRegion(Brushes.Red, gridregion[7]); endofgame = 1; } if (win6 == 6) { g.DrawString(Userwins, winfont, Brushes.Black, w / 20 * 10, h / 20 * 17); g.FillRegion(Brushes.Red, gridregion[2]); g.FillRegion(Brushes.Red, gridregion[5]); g.FillRegion(Brushes.Red, gridregion[8]); endofgame = 1; } if (win7 == 6) { g.DrawString(Userwins, winfont, Brushes.Black, w / 20 * 10, h / 20 * 17); g.FillRegion(Brushes.Red, gridregion[0]); g.FillRegion(Brushes.Red, gridregion[4]); g.FillRegion(Brushes.Red, gridregion[8]); endofgame = 1; } if (win8 == 6) { g.DrawString(Userwins, winfont, Brushes.Black, w / 20 * 10, h / 20 * 17); g.FillRegion(Brushes.Red, gridregion[2]); g.FillRegion(Brushes.Red, gridregion[4]); g.FillRegion(Brushes.Red, gridregion[6]); endofgame = 1; } if (win1 == 15) { g.DrawString(CompWins, wincompfont, Brushes.Black, w / 20 * 9, h / 20 * 17 + 10); g.FillRegion(Brushes.Red, gridregion[0]); g.FillRegion(Brushes.Red, gridregion[1]); g.FillRegion(Brushes.Red, gridregion[2]); endofgame = 1; } if (win2 == 15) { g.DrawString(CompWins, wincompfont, Brushes.Black, w / 20 * 9, h / 20 * 17 + 10); g.FillRegion(Brushes.Red, gridregion[3]); g.FillRegion(Brushes.Red, gridregion[4]); g.FillRegion(Brushes.Red, gridregion[5]); endofgame = 1; } if (win3 == 15) { g.DrawString(CompWins, wincompfont, Brushes.Black, w / 20 * 9, h / 20 * 17 + 10); g.FillRegion(Brushes.Red, gridregion[6]); g.FillRegion(Brushes.Red, gridregion[7]); g.FillRegion(Brushes.Red, gridregion[8]); endofgame = 1; } if (win4 == 15) { g.DrawString(CompWins, wincompfont, Brushes.Black, w / 20 * 9, h / 20 * 17 + 10); g.FillRegion(Brushes.Red, gridregion[0]); g.FillRegion(Brushes.Red, gridregion[3]); g.FillRegion(Brushes.Red, gridregion[6]); endofgame = 1; } if (win5 == 15) { g.DrawString(CompWins, wincompfont, Brushes.Black, w / 20 * 9, h / 20 * 17+ 10); g.FillRegion(Brushes.Red, gridregion[1]); g.FillRegion(Brushes.Red, gridregion[4]); endofgame = 1; g.FillRegion(Brushes.Red, gridregion[7]); } if (win6 == 15) { g.DrawString(CompWins, wincompfont, Brushes.Black, w / 20 * 9, h / 20 * 17 + 10); g.FillRegion(Brushes.Red, gridregion[2]); g.FillRegion(Brushes.Red, gridregion[5]); g.FillRegion(Brushes.Red, gridregion[8]); endofgame = 1; } if (win7 == 15) { g.DrawString(CompWins, wincompfont, Brushes.Black, w / 20 * 9, h / 20 * 17 + 10); g.FillRegion(Brushes.Red, gridregion[0]); g.FillRegion(Brushes.Red, gridregion[4]); g.FillRegion(Brushes.Red, gridregion[8]); endofgame = 1; } if (win8 == 15) { g.DrawString(CompWins, wincompfont, Brushes.Black, w / 20 * 9, h / 20 * 17 + 10); g.FillRegion(Brushes.Red, gridregion[2]); g.FillRegion(Brushes.Red, gridregion[4]); g.FillRegion(Brushes.Red, gridregion[6]); endofgame = 1; } // see if there is draw if (maxturn == 9) { if (endofgame == 0) { g.DrawString("Draw!", winfont, Brushes.Black, w / 20 * 10, h / 20 * 17); } } } public void ArrayScores(Graphics g) { //////////////////////////////////////////////// // create array - if userturn then value = 2, if comp turn then value = 4, // if loop - if array == 2 draw in X if array ==4 draw in O int w = DisplayRectangle.Width; int h = DisplayRectangle.Height; int fontsize = (w / 20 * 3); Font arial = new Font("Arial", fontsize, FontStyle.Regular); for (int x = 0; x < 9; x++) { if (XOvalues[x] == 2) { if (checkedboxes[x] == 0 && XOvalues[x] == 2) { g.DrawString("X", arial, Brushes.Black, w / 20 + 15, h / 20 - 10, StringFormat.GenericDefault); squareempty[x] = 1; } else if (checkedboxes[x] == 1 && XOvalues[x] == 2) { g.DrawString("X", arial, Brushes.Black, w / 20 * 7 + 15, h / 20 - 10, StringFormat.GenericDefault); } else if (checkedboxes[x] == 2 && XOvalues[x] == 2) { g.DrawString("X", arial, Brushes.Black, w / 20 * 13 + 15, h / 20 - 10, StringFormat.GenericDefault); } else if (checkedboxes[x] == 3 && XOvalues[x] == 2) { g.DrawString("X", arial, Brushes.Black, w / 20 + 20, h / 20 * 6 - 10, StringFormat.GenericDefault); } else if (checkedboxes[x] == 4 && XOvalues[x] == 2) { g.DrawString("X", arial, Brushes.Black, w / 20 * 7 + 20, h / 20 * 6 - 10, StringFormat.GenericDefault); } else if (checkedboxes[x] == 5 && XOvalues[x] == 2) { g.DrawString("X", arial, Brushes.Black, w / 20 * 13 + 15, h / 20 * 6 - 10, StringFormat.GenericDefault); } else if (checkedboxes[x] == 6 && XOvalues[x] == 2) { g.DrawString("X", arial, Brushes.Black, w / 20 + 20, h / 20 * 11 - 15, StringFormat.GenericDefault); } else if (checkedboxes[x] == 7 && XOvalues[x] == 2) { g.DrawString("X", arial, Brushes.Black, w / 20 * 7 + 20, h / 20 * 11 - 15, StringFormat.GenericDefault); } else if (checkedboxes[x] == 8 && XOvalues[x] == 2) { g.DrawString("X", arial, Brushes.Black, w / 20 * 13 + 20, h / 20 * 11 - 15, StringFormat.GenericDefault); } } else if (XOvalues[x] == 5) { if (checkedboxes[x] == 0 && XOvalues[x] == 5) { g.DrawString("O", arial, Brushes.Black, w / 20 + 15, h / 20 - 10, StringFormat.GenericDefault); squareempty[x] = 1; } else if (checkedboxes[x] == 1 && XOvalues[x] == 5) { g.DrawString("O", arial, Brushes.Black, w / 20 * 7 + 15, h / 20 - 10, StringFormat.GenericDefault); } else if (checkedboxes[x] == 2 && XOvalues[x] == 5) { g.DrawString("O", arial, Brushes.Black, w / 20 * 13 + 15, h / 20 - 10, StringFormat.GenericDefault); } else if (checkedboxes[x] == 3 && XOvalues[x] == 5) { g.DrawString("O", arial, Brushes.Black, w / 20 + 20, h / 20 * 6 - 10, StringFormat.GenericDefault); } else if (checkedboxes[x] == 4 && XOvalues[x] == 5) { g.DrawString("O", arial, Brushes.Black, w / 20 * 7 + 20, h / 20 * 6 - 10, StringFormat.GenericDefault); } else if (checkedboxes[x] == 5 && XOvalues[x] == 5) { g.DrawString("O", arial, Brushes.Black, w / 20 * 13 + 15, h / 20 * 6 - 10, StringFormat.GenericDefault); } else if (checkedboxes[x] == 6 && XOvalues[x] == 5) { g.DrawString("O", arial, Brushes.Black, w / 20 + 20, h / 20 * 11 - 15, StringFormat.GenericDefault); } else if (checkedboxes[x] == 7 && XOvalues[x] == 5) { g.DrawString("O", arial, Brushes.Black, w / 20 * 7 + 20, h / 20 * 11 - 15, StringFormat.GenericDefault); } else if (checkedboxes[x] == 8 && XOvalues[x] == 5) { g.DrawString("O", arial, Brushes.Black, w / 20 * 13 + 20, h / 20 * 11 - 15, StringFormat.GenericDefault); } } } } protected override void onmousedown(MouseEventArgs e) { //////////////////////////////////// // reset button ///////////////////////////////////// RectangleF resetrect = resetregion.GetBounds(CreateGraphics()); if (resetrect.Contains(e.X, e.Y)) { //start = true; //Invalidate(); Application.Restart(); } ///////////////////////////////////// // click in squares //////////////////////////////////// Boolean found = false; int i = 0; while (!found && i < gridregion.Length && endofgame == 0) { RectangleF[] scans = gridregion[i].GetRegionScans(new Matrix()); int j = 0; while (!found && j < scans.Length) { if (scans[j].Contains(e.X, e.Y) && XOvalues[i] == 0) { index = i; found = true; } j++; } i++; } ///////////////////////////////////////////// // Invalidate(); base.onmousedown(e); } protected override void onmouseup(MouseEventArgs e) { if (endofgame == 0) { if (index < 10 && whosturn == 1 && XOvalues[index] == 0) { XOvalues[index] = 2; checkedboxes[index] = index; wongame[index] = 2; whosturn = 0; maxturn++; Invalidate(); } } base.onmouseup(e); } static void Main() { // NewGame createnewgame = new NewGame(); Application.Run(new TicTacToe()); } }
i kind of understand the setup of minimax loops but ye i dont understand things like
a) am i defining max as 1 and min as -1
b)alpha and beta are basically "posh" words for max and min in the loops?
c) i dont get how to work out a available moves "score"
thanks
This post has been edited by bboyzeez: 18 January 2013 - 11:02 AM