I have recently started using Unity 5 and C# and I have created a turn based game that connects 5 in a row using GUI. I am trying to implement an AI that will make a random move (for now) without me clicking on the GUI tiles on the board to start my minimax. Right now I am controlling player 1 and player 2 manually by pressing the tiles down that was created using the GUI.
The problem is I don’t know how to convert a player into such an AI. I have so far created 2 players that are x and o and have turns but I have no idea on how to approach creating one of the player as an AI.
public void Level(){//level
Rect titleRect = new Rect (0, 0, 300, 75);
GUI.Label(titleRect, "Level");
Rect multiRect = new Rect (titleRect.x, titleRect.y + titleRect.height, titleRect.width, 75);
Rect multiRect2 = new Rect (titleRect.x, titleRect.y + 100 + titleRect.height, titleRect.width, 75);
Rect multiRect3 = new Rect (titleRect.x, titleRect.y + 200 + titleRect.height, titleRect.width, 75);
if (GUI.Button (multiRect, "Easy")) {
board = new State[5,5];
gameState = GameState.Player;
w = 30;
h = 30;
}
}
public void Board(){
//Easy
for (y = 0; y < board.GetLength(0); y++) {
for (x = 0; x < board.GetLength(1); x++) {
boardIndex = y;
boardIndey = x;
drawBoard ();
}
}
}
public void drawBoard(){//draw the board
Rect SQUARE = new Rect (x * w, y * h, w, h);
string player = board [boardIndex, boardIndey] == State.P1 ? "X" :
board [boardIndex, boardIndey] == State.P2 ? "O" : " ";
if (board [boardIndex, boardIndey] == State.D) {
if (GUI.Button (SQUARE, player))
SetControl (boardIndex);
isMovesLeft ();
} else
GUI.Label (SQUARE, player, player + "style");
}
public void SetControl(int boardIndex){//switch player
if (boardIndex < 0 || boardIndex >= board.Length)
return;
board [boardIndex, boardIndey] = p1Turn ? State.P1 : State.P2;
p1Turn = !p1Turn;
}
public enum State{
D,
P1,
P2,
AI,
}
Try just … after it’s your turn, call a method that picks a valid number and places a piece, then sets it back to your turn.
From there, if that’s working, develop a more sophisticated logic for what choice the computer might make?
OK, first, I’m sorry to say, you must have found a very old tutorial or other resource, because you’ve gone about this in a way that is no longer recommended (using GUI.Button, GUI.Label, etc.). The good news is, you’re not very far in; please start over and use the modern UI system, per these tutorials.
Then, be sure your detection of player input is separate from your game logic. The new UI system makes this falling-off-a-log easy. A click on a button, if it’s the player’s turn, will just invoke some public method indicating the player wants to go there. Your AI script will be able to call exactly the same public method, without going through the UI.
Hello I did this way because I wanted to implement minimax with the ai is it still possible to implement minimax do it creating with the new UI system, like in terms of putting the AI in. Since I am currently using 2d array for my winning logic and i want to set the buttons to 2d array rather than having single array because it is easier for me to work with rather than having buttons that actually is hardcoded because I need to do this for like different level sized boards and make the winning condition work for every single board. I had looked at the tutorial and button seems hardcoded. Your reply would be much help me dealing with this problem.
I’m sorry, I don’t understand. You’re already using a 2D array, which is a good idea (it’s the proper model for your board). This has (or should have) nothing to do with the UI.