c# - Tic tac toe vs computer class -
i writing tic tac toe game. logic in class that's why have difficulties. board matrix. firstly, put x when click button, computer choose block put o. in class save value of matrix element don't know how show on winform. here part of code..
public string togglenextletter() { if (!changedletterx) { changedletterx = true; return this.nextletter = "x"; } changedletterx = false; movecomputer(); return this.nextletter = "o"; }
//reserve position of x form
public string[,] matrix(int row, int col, string value) { this.field[row, col] = value; return this.field; } public string[,] movecomputer() { //priority 1: tick tac toe //priority 2: block x tic tac toe //priority 3: go corner space //priority 4: pick open space string move; move = lookforwinorblock("o"); //look win if(move == null) { move = lookforwinorblock("x"); //look block if (move == null) { move = lookforcorner(); if (move == null) { move = lookforopenspace(); } } } return move; } private void btn_click(object sender, eventargs e) { button btn = (button)sender; int btnrow = convert.toint32(btn.tag) / 10; int btncol = convert.toint32(btn.tag) % 10; btn.text = game.togglenextletter(); game.matrix(btnrow, btncol, btn.text); btn.enabled = false; //btn.text = game.togglenextletter(); if (game.haswinner()) { foreach (var control in controls) { try { button b = (button)control; b.enabled = false; } catch { } } messagebox.show("the winner " + btn.text); } }
just separate functions
public string[,] movecomputer() { // add line makeplay(btnrow, btncol, btntext); return move; } private void btn_click(object sender, eventargs e) { button btn = (button)sender; int btnrow = convert.toint32(btn.tag) / 10; int btncol = convert.toint32(btn.tag) % 10; btn.text = game.togglenextletter(); makeplay((btnrow, btncol, btn.text); } private void makeplay(int btnrow, int btncol, string btntext) { game.matrix(btnrow, btncol, btn.text); btn.enabled = false; //btn.text = game.togglenextletter(); if (game.haswinner()) { foreach (var control in controls) { try { button b = (button)control; b.enabled = false; } catch { } } messagebox.show("the winner " + btn.text); } }
Comments
Post a Comment