private bool validate(MoveLocation value) { if (board.getTilevalue(value) == 0) { return(true); } return(false); }
private bool turnHuman(MoveLocation position) { if (isXturn) { if (validate(position)) { if (player1.PlayerTile == 1) { player1.setmove(position); player1.move(board); } else { player2.setmove(position); player2.move(board); } TileChanged(position, "X"); } else { MessageBox.Show("invalid p1"); isValidMove = false; return(false);; } } else { if (validate(position)) { if (player2.PlayerTile == -1) { player2.setmove(position); player2.move(board); } else { player1.setmove(position); player1.move(board); } TileChanged(position, "O"); } else { MessageBox.Show("invalid p2"); isValidMove = false; return(false); } } turnCount++; return(true); }
private MoveLocation getMoveByName(string name) { MoveLocation move = null; for (int i = 0; i < 9; i++) { if (name.Equals("btnGrid" + (i + 1))) { move = new MoveLocation(i % 3, i / 3); } } return(move); }
private int[] minimax(int depth, int tile, GameBoard dummyBoard) { // Generate possible next moves in a List of int[2] of {row, col}. List <MoveLocation> nextMoves = generateNextPossibleMoves(dummyBoard); // mySeed is maximizing; while oppSeed is minimizing int bestScore = (tile == PlayerTile) ? Int32.MinValue : Int32.MaxValue; int currentScore; int bestRow = -1; int bestCol = -1; if (nextMoves.Count == 0 || depth == 0) { // Gameover or depth reached, evaluate score bestScore = evaluate(dummyBoard); } else { for (int i = 0; i < nextMoves.Count; i++) { MoveLocation move = nextMoves[i]; // Try this move for the current "player" dummyBoard.placeTile(move, tile); if (tile == PlayerTile) { // mySeed (computer) is maximizing player currentScore = minimax(depth - 1, -PlayerTile, dummyBoard)[0]; if (currentScore > bestScore) { bestScore = currentScore; bestRow = move.X; bestCol = move.Y; } } else { // oppSeed is minimizing player currentScore = minimax(depth - 1, PlayerTile, dummyBoard)[0]; if (currentScore < bestScore) { bestScore = currentScore; bestRow = move.X; bestCol = move.Y; } } // Undo move dummyBoard.placeTile(move, 0); } } return(new int[] { bestScore, bestRow, bestCol }); }
private List <MoveLocation> generateNextPossibleMoves(GameBoard board) { List <MoveLocation> nextPossibleMoves = new List <MoveLocation>(); for (int i = 0; i < 3; i++) { for (int j = 0; j < 3; j++) { MoveLocation testMove = new MoveLocation(i, j); if (board.getTilevalue(testMove) == 0) { nextPossibleMoves.Add(testMove); } } } return(nextPossibleMoves); }
public void taketurn(MoveLocation position) { if (isAI) { if (!turnComputer(position)) { return; } } else { if (!turnHuman(position)) { return; } checkForWinners(); isXturn = !isXturn; } isValidMove = true; }
public void setmove(MoveLocation newMove) { mymove = newMove; }
public Player(string name, int tileType) { playerName = name; playerTile = tileType; mymove = new MoveLocation(0, 0); }
private bool turnComputer(MoveLocation position) { if (validate(position)) { player1.setmove(position); player1.move(board); if (player1.PlayerTile == 1) { TileChanged(position, "X"); } else { TileChanged(position, "O"); } } else { MessageBox.Show("invalid p1"); isValidMove = false; return(false);; } turnCount++; checkForWinners(); if (player1.PlayerTile == 1) { isXturn = false; } else { isXturn = true; } isValidMove = true; if (hasWinner) { return(true); } MoveLocation cpuMove; do { cpuMove = ((ComputerPlayer)player2).getMove(board); } while (turnCount != 9 && !validate(cpuMove)); if (turnCount != 9) { player2.setmove(cpuMove); player2.move(board); if (player2.PlayerTile == -1) { TileChanged(cpuMove, "O"); } else { TileChanged(cpuMove, "X"); } turnCount++; checkForWinners(); if (player2.PlayerTile == -1) { isXturn = true; } else { isXturn = false; } } return(true); }
private void controller_TileChanged(MoveLocation location, string tileValue) { buttonGrid[location.X, location.Y].BackgroundImage = tileValue.Equals("X")? Properties.Resources.NEW_x:Properties.Resources.new_o; buttonGrid[location.X, location.Y].Enabled = false; //buttonGrid[location.X, location.Y].BackColor = Color.Yellow; }
public int getTilevalue(MoveLocation pos) { return(tiles[pos.X, pos.Y]); }
public void placeTile(MoveLocation place, int type) { tiles[place.X, place.Y] = type; }