internal CheckersPiece(CheckersGame owner, int player, CheckersRank rank, Point location, bool inPlay) { if((player < 1) || (player > 2)) throw new ArgumentOutOfRangeException("player", player, "Argument 'player' must be a valid player number (a number greater or equal to 1)."); if((location.X < 0) || (location.X >= CheckersGame.BoardSize.Width) || (location.Y < 0) || (location.Y >= CheckersGame.BoardSize.Height)) throw new ArgumentOutOfRangeException("location", player, "Argument 'location' must be a valid position on the board."); this.owner = owner; this.player = player; this.rank = rank; this.location = location; this.inPlay = inPlay; }
internal CheckersPiece(CheckersGame owner, int player, CheckersRank rank, Point location, bool inPlay) { if ((player < 1) || (player > 2)) { throw new ArgumentOutOfRangeException("player", player, "Argument 'player' must be a valid player number (a number greater or equal to 1)."); } if ((location.X < 0) || (location.X >= CheckersGame.BoardSize.Width) || (location.Y < 0) || (location.Y >= CheckersGame.BoardSize.Height)) { throw new ArgumentOutOfRangeException("location", player, "Argument 'location' must be a valid position on the board."); } this.owner = owner; this.player = player; this.rank = rank; this.location = location; this.inPlay = inPlay; }
/// <summary>Creates a duplicate Checkers piece object from an identical (possibly cloned) game.</summary> /// <returns>The new Checkers piece object.</returns> public CheckersPiece Clone(CheckersGame game) { CheckersPiece clonedPiece = game.PieceAt(Location); // Make sure piece exists if (clonedPiece == null) { return(null); } // Make sure piece is equivalent if ((clonedPiece.Player != Player) || (clonedPiece.InPlay != InPlay) || (clonedPiece.Rank != Rank)) { return(null); } // Return cloned piece return(clonedPiece); }
public void test_scoreGame() { CheckersGame game = new CheckersGame(); //Start with a blank board for (int i = 0; i < 32; i++) game.squares[i].squareType = CheckerType.empty; game.squares[4].squareType = CheckerType.whiteKing; game.squares[0].squareType = CheckerType.whiteChecker; game.squares[9].squareType = CheckerType.whiteChecker; game.squares[14].squareType = CheckerType.whiteChecker; game.squares[10].squareType = CheckerType.redChecker; game.squares[29].squareType = CheckerType.redChecker; game.squares[17].squareType = CheckerType.redKing; game.squares[16].squareType = CheckerType.redKing; double score = game.scoreGame(game, false); double epsilon = 0.001; Assert.IsTrue(epsilon > Math.Abs(score - 0.583333)); }
/// <summary>Creates a Checkers game from supplied game parameters.</summary> /// <param name="optionalJumping">The Optional Jumping rule.</param> /// <param name="board">The Checkers board that makes up the game.</param> /// <param name="turn">Whose turn it is.</param> /// <param name="winner">The winner, or 0 if none yet.</param> /// <returns>The Checkers game.</returns> public static CheckersGame Create(bool optionalJumping, CheckersPiece[,] board, int turn, int winner) { if ((board.GetLength(0) != BoardSize.Width) || (board.GetLength(1) != BoardSize.Height)) { throw new ArgumentOutOfRangeException("board", board, "Board's dimensions must be " + BoardSize.Width + "x" + BoardSize.Height); } CheckersGame game = new CheckersGame(optionalJumping); game.board = new CheckersPiece[BoardSize.Width, BoardSize.Height]; game.pieces = new CheckersPieceCollection(); for (int y = 0; y < BoardSize.Height; y++) { for (int x = 0; x < BoardSize.Width; x++) { CheckersPiece piece = board[x, y]; if (piece == null) { continue; } if (piece.Owner != null) { throw new ArgumentOutOfRangeException("board", board, "Board contains a Checkers piece that belongs to another Checkers game."); } if (!piece.InPlay) { throw new ArgumentOutOfRangeException("board", board, "Board contains a Checkers piece that is not in play."); } if ((piece.Location.X != x) || (piece.Location.Y != y)) { throw new ArgumentOutOfRangeException("board", board, "Board contains a Checkers piece that does not match up with it's board location."); } if ((piece.Player != 1) || (piece.Player != 2)) { throw new ArgumentOutOfRangeException("board", board, "Board contains a Checkers piece that is not associated with a valid player."); } game.pieces.Add(new CheckersPiece(game, piece.Player, piece.Rank, piece.Location, piece.InPlay)); } } game.isPlaying = true; game.turn = turn; game.winner = winner; return(game); }
private Boolean isLegalMove(CheckersGame game, Square origin, MoveDirs destination) { var stop = (destination == MoveDirs.TLEFT ? origin.tl : (destination == MoveDirs.TRIGHT ? origin.tr : (destination == MoveDirs.BLEFT ? origin.bl : origin.br))); if (stop == null || stop.squareType != CheckerType.empty) { return(false); } if (origin.squareType == CheckerType.whiteKing || origin.squareType == CheckerType.redKing) { return(true); } else if (origin.squareType == CheckerType.whiteChecker) { if (stop == origin.tl || stop == origin.tr) { return(true); } else { return(false); } } else if (origin.squareType == CheckerType.redChecker) { if (stop == origin.bl || stop == origin.br) { return(true); } else { return(false); } } else { return(false); } }
/// <summary>Creates a duplicate Checkers game object.</summary> /// <returns>The new Checkers move object.</returns> public CheckersGame Clone() { CheckersGame game = new CheckersGame(optionalJumping); game.isReadOnly = isReadOnly; game.isPlaying = isPlaying; game.firstMove = firstMove; game.turn = turn; game.winner = winner; game.pieces = new CheckersPieceCollection(); game.board = new CheckersPiece[BoardSize.Width, BoardSize.Height]; foreach (CheckersPiece piece in pieces) { CheckersPiece newPiece = new CheckersPiece(game, piece.Player, piece.Rank, piece.Location, piece.InPlay); game.board[newPiece.Location.X, newPiece.Location.Y] = newPiece; game.pieces.Add(newPiece); } int lastMovePieceIndex = ((lastMove != null) ? (pieces.IndexOf(lastMove.Piece)) : (-1)); game.lastMove = ((lastMovePieceIndex != -1) ? (CheckersMove.FromPath(game, game.pieces[lastMovePieceIndex], lastMove.Path)) : (null)); return(game); }
private MoveDirs canCapture(CheckersGame game, Square origin) { MoveDirs capDirs = MoveDirs.NONE; if (origin.tl != null && origin.tl.tl != null && isLegalCapture(game, origin, MoveDirs.TLEFT)) { capDirs = capDirs | MoveDirs.TLEFT; } if (origin.tr != null && origin.tr.tr != null && isLegalCapture(game, origin, MoveDirs.TRIGHT)) { capDirs = capDirs | MoveDirs.TRIGHT; } if (origin.bl != null && origin.bl.bl != null && isLegalCapture(game, origin, MoveDirs.BLEFT)) { capDirs = capDirs | MoveDirs.BLEFT; } if (origin.br != null && origin.br.br != null && isLegalCapture(game, origin, MoveDirs.BRIGHT)) { capDirs = capDirs | MoveDirs.BRIGHT; } return(capDirs); }
public void singleCapture(CheckersGame game, Square origin, MoveDirs capDir) { switch (capDir) { case MoveDirs.TLEFT: origin.tl.tl.squareType = origin.squareType; origin.tl.squareType = CheckerType.empty; origin.squareType = CheckerType.empty; kingMe(game, origin.tl.tl); break; case MoveDirs.TRIGHT: origin.tr.tr.squareType = origin.squareType; origin.tr.squareType = CheckerType.empty; origin.squareType = CheckerType.empty; kingMe(game, origin.tr.tr); break; case MoveDirs.BLEFT: origin.bl.bl.squareType = origin.squareType; origin.bl.squareType = CheckerType.empty; origin.squareType = CheckerType.empty; kingMe(game, origin.bl.bl); break; case MoveDirs.BRIGHT: origin.br.br.squareType = origin.squareType; origin.br.squareType = CheckerType.empty; origin.squareType = CheckerType.empty; kingMe(game, origin.br.br); break; default: break; } }
/// <summary>Returns a read-only game from the original game.</summary> /// <param name="game">The game to copy and make read-only.</param> /// <returns>The read-only copy.</returns> public static CheckersGame MakeReadOnly(CheckersGame game) { CheckersGame result = game.Clone(); result.isReadOnly = true; return result; }
/// <summary>Returns the best possible next move using the current move-finding technique.</summary> /// <returns>The next best possible move.</returns> public abstract CheckersMove NextMove(CheckersGame game);
public AgentTickEventArgs() { this.game = null; }
public double scoreGame(CheckersGame game, Boolean isWhite) { int i, whiteRowNum, redRowNum; double whiteScore, redScore, scoreSum, score; whiteScore = 0.0; redScore = 0.0; for (i = 0; i < 32; i++) { whiteRowNum = i / 4; redRowNum = 7 - whiteRowNum; //Calculate white portion of score if (game.squares[i].squareType == CheckerType.whiteChecker) whiteScore += 1 + (whiteRowNum / 2); else if (game.squares[i].squareType == CheckerType.redChecker) redScore += (1 + (redRowNum / 2)); else if (game.squares[i].squareType == CheckerType.whiteKing) whiteScore += 5; else if (game.squares[i].squareType == CheckerType.redKing) redScore += 5; else continue; } scoreSum = whiteScore + redScore; if (whiteScore == 0) return isWhite ? -1.0 : 1.0; else if (redScore == 0) return isWhite ? 1.0 : -1.0; else if (whiteScore > redScore) { score = whiteScore / scoreSum; if (isWhite) return score; else return -score; } else if (redScore > whiteScore) { score = redScore / scoreSum; if (isWhite) return -score; else return score; } //Scores are equal and non-zero. else return 0.0; }
/** * @param capDir MoveDirs single direction * @return whether proposed capture is legal */ private Boolean isLegalCapture(CheckersGame game, Square origin, MoveDirs capDir, Square ignore = null) { Boolean isWhite = ( (origin.squareType == CheckerType.whiteChecker || origin.squareType == CheckerType.whiteKing) ? true : false ); Square toCapture, stop; if (capDir == MoveDirs.TLEFT) { if (origin.squareType == CheckerType.redChecker) return false; stop = origin.tl.tl; toCapture = origin.tl; } else if (capDir == MoveDirs.TRIGHT) { if (origin.squareType == CheckerType.redChecker) return false; stop = origin.tr.tr; toCapture = origin.tr; } else if (capDir == MoveDirs.BLEFT) { if (origin.squareType == CheckerType.whiteChecker) return false; stop = origin.bl.bl; toCapture = origin.bl; } else if (capDir == MoveDirs.BRIGHT) { if (origin.squareType == CheckerType.whiteChecker) return false; stop = origin.br.br; toCapture = origin.br; } else return false; if (toCapture == null || stop == null) return false; if (toCapture.squareType == CheckerType.empty) return false; if (stop.squareType != CheckerType.empty && stop != ignore) return false; if ((toCapture.squareType == CheckerType.redChecker || toCapture.squareType == CheckerType.redKing) ^ isWhite) return false; return true; }
private MoveDirs canCapture(CheckersGame game, Square origin) { MoveDirs capDirs = MoveDirs.NONE; if (origin.tl != null && origin.tl.tl != null && isLegalCapture(game, origin, MoveDirs.TLEFT)) capDirs = capDirs | MoveDirs.TLEFT; if (origin.tr != null && origin.tr.tr != null && isLegalCapture(game, origin, MoveDirs.TRIGHT)) capDirs = capDirs | MoveDirs.TRIGHT; if (origin.bl != null && origin.bl.bl != null && isLegalCapture(game, origin, MoveDirs.BLEFT)) capDirs = capDirs | MoveDirs.BLEFT; if (origin.br != null && origin.br.br != null && isLegalCapture(game, origin, MoveDirs.BRIGHT)) capDirs = capDirs | MoveDirs.BRIGHT; return capDirs; }
public void move(CheckersGame game, Square origin, Square destination) { destination.squareType = origin.squareType; origin.squareType = CheckerType.empty; kingMe(game, destination); }
public void test_initGame() { CheckersGame game = new CheckersGame(); Assert.IsTrue(game.squares[0].squareType == CheckerType.whiteChecker); }
public double scoreGame(CheckersGame game, Boolean isWhite) { int i, whiteRowNum, redRowNum; double whiteScore, redScore, scoreSum, score; whiteScore = 0.0; redScore = 0.0; for (i = 0; i < 32; i++) { whiteRowNum = i / 4; redRowNum = 7 - whiteRowNum; //Calculate white portion of score if (game.squares[i].squareType == CheckerType.whiteChecker) { whiteScore += 1 + (whiteRowNum / 2); } else if (game.squares[i].squareType == CheckerType.redChecker) { redScore += (1 + (redRowNum / 2)); } else if (game.squares[i].squareType == CheckerType.whiteKing) { whiteScore += 5; } else if (game.squares[i].squareType == CheckerType.redKing) { redScore += 5; } else { continue; } } scoreSum = whiteScore + redScore; if (whiteScore == 0) { return(isWhite ? -1.0 : 1.0); } else if (redScore == 0) { return(isWhite ? 1.0 : -1.0); } else if (whiteScore > redScore) { score = whiteScore / scoreSum; if (isWhite) { return(score); } else { return(-score); } } else if (redScore > whiteScore) { score = redScore / scoreSum; if (isWhite) { return(-score); } else { return(score); } } //Scores are equal and non-zero. else { return(0.0); } }
public Boolean checkMove(CheckersGame game, Square origin, Square destination, Boolean mustJump = false) { if (origin.tl != null && destination == origin.tl && !mustJump) { if (isLegalMove(game, origin, MoveDirs.TLEFT)) { move(game, origin, destination); return(true); } else { return(false); } } else if (origin.tr != null && destination == origin.tr && !mustJump) { if (isLegalMove(game, origin, MoveDirs.TRIGHT)) { move(game, origin, destination); return(true); } else { return(false); } } else if (origin.bl != null && destination == origin.bl && !mustJump) { if (isLegalMove(game, origin, MoveDirs.BLEFT)) { move(game, origin, destination); return(true); } else { return(false); } } else if (origin.br != null && destination == origin.br && !mustJump) { if (isLegalMove(game, origin, MoveDirs.BRIGHT)) { move(game, origin, destination); return(true); } else { return(false); } } else if (origin.tl != null && origin.tl.tl != null && destination == origin.tl.tl) { if (isLegalCapture(game, origin, MoveDirs.TLEFT)) { singleCapture(game, origin, MoveDirs.TLEFT); } else { return(false); } } else if (origin.tr != null && origin.tr.tr != null && destination == origin.tr.tr) { if (isLegalCapture(game, origin, MoveDirs.TRIGHT)) { singleCapture(game, origin, MoveDirs.TRIGHT); } else { return(false); } } else if (origin.bl != null && origin.bl.bl != null && destination == origin.bl.bl) { if (isLegalCapture(game, origin, MoveDirs.BLEFT)) { singleCapture(game, origin, MoveDirs.BLEFT); } else { return(false); } } else if (origin.br != null && origin.br.br != null && destination == origin.br.br) { if (isLegalCapture(game, origin, MoveDirs.BRIGHT)) { singleCapture(game, origin, MoveDirs.BRIGHT); } else { return(false); } } else { return(false); } //Check for multiple jump if (canCapture(game, destination) == MoveDirs.NONE) { jumpRequired = false; multipleJumpInProgress = false; return(true); } else { originSquare = destination; jumpRequired = true; multipleJumpInProgress = true; multipleJumper = destination; return(false); } }
/** * @param capDir MoveDirs single direction * @return whether proposed capture is legal */ private Boolean isLegalCapture(CheckersGame game, Square origin, MoveDirs capDir, Square ignore = null) { Boolean isWhite = ( (origin.squareType == CheckerType.whiteChecker || origin.squareType == CheckerType.whiteKing) ? true : false ); Square toCapture, stop; if (capDir == MoveDirs.TLEFT) { if (origin.squareType == CheckerType.redChecker) { return(false); } stop = origin.tl.tl; toCapture = origin.tl; } else if (capDir == MoveDirs.TRIGHT) { if (origin.squareType == CheckerType.redChecker) { return(false); } stop = origin.tr.tr; toCapture = origin.tr; } else if (capDir == MoveDirs.BLEFT) { if (origin.squareType == CheckerType.whiteChecker) { return(false); } stop = origin.bl.bl; toCapture = origin.bl; } else if (capDir == MoveDirs.BRIGHT) { if (origin.squareType == CheckerType.whiteChecker) { return(false); } stop = origin.br.br; toCapture = origin.br; } else { return(false); } if (toCapture == null || stop == null) { return(false); } if (toCapture.squareType == CheckerType.empty) { return(false); } if (stop.squareType != CheckerType.empty && stop != ignore) { return(false); } if ((toCapture.squareType == CheckerType.redChecker || toCapture.squareType == CheckerType.redKing) ^ isWhite) { return(false); } return(true); }
public AgentTickEventArgs(CheckersGame game) { this.game = game; }
private Boolean isLegalMove(CheckersGame game, Square origin, MoveDirs destination) { var stop = (destination == MoveDirs.TLEFT ? origin.tl : (destination == MoveDirs.TRIGHT ? origin.tr : (destination == MoveDirs.BLEFT ? origin.bl : origin.br))); if (stop == null || stop.squareType != CheckerType.empty) return false; if (origin.squareType == CheckerType.whiteKing || origin.squareType == CheckerType.redKing) { return true; } else if (origin.squareType == CheckerType.whiteChecker) { if (stop == origin.tl || stop == origin.tr) return true; else return false; } else if (origin.squareType == CheckerType.redChecker) { if (stop == origin.bl || stop == origin.br) return true; else return false; } else return false; }
public Boolean checkMove(CheckersGame game, Square origin, Square destination, Boolean mustJump = false) { if (origin.tl != null && destination == origin.tl && !mustJump) if (isLegalMove(game, origin, MoveDirs.TLEFT)) { move(game, origin, destination); return true; } else return false; else if (origin.tr != null && destination == origin.tr && !mustJump) if (isLegalMove(game, origin, MoveDirs.TRIGHT)) { move(game, origin, destination); return true; } else return false; else if (origin.bl != null && destination == origin.bl && !mustJump) if (isLegalMove(game, origin, MoveDirs.BLEFT)) { move(game, origin, destination); return true; } else return false; else if (origin.br != null && destination == origin.br && !mustJump) { if (isLegalMove(game, origin, MoveDirs.BRIGHT)) { move(game, origin, destination); return true; } else return false; } else if (origin.tl != null && origin.tl.tl != null && destination == origin.tl.tl) { if (isLegalCapture(game, origin, MoveDirs.TLEFT)) { singleCapture(game, origin, MoveDirs.TLEFT); } else return false; } else if (origin.tr != null && origin.tr.tr != null && destination == origin.tr.tr) { if (isLegalCapture(game, origin, MoveDirs.TRIGHT)) { singleCapture(game, origin, MoveDirs.TRIGHT); } else return false; } else if (origin.bl != null && origin.bl.bl != null && destination == origin.bl.bl) { if (isLegalCapture(game, origin, MoveDirs.BLEFT)) { singleCapture(game, origin, MoveDirs.BLEFT); } else return false; } else if (origin.br != null && origin.br.br != null && destination == origin.br.br) if (isLegalCapture(game, origin, MoveDirs.BRIGHT)) { singleCapture(game, origin, MoveDirs.BRIGHT); } else return false; else return false; //Check for multiple jump if (canCapture(game, destination) == MoveDirs.NONE) { jumpRequired = false; multipleJumpInProgress = false; return true; } else { originSquare = destination; jumpRequired = true; multipleJumpInProgress = true; multipleJumper = destination; return false; } }
protected void OnTick(CheckersGame game) { if(Tick != null) Tick(this, new AgentTickEventArgs(game)); }
public static void Main() { CheckersGame.RunGame(); }
/// <summary> /// Starts a new game. /// </summary> private void DoNewGame() { if ((CheckersUI.IsPlaying) || (CheckersUI.Winner != 0)) { if (!DoCloseGame()) { return; } // Stop current game (with no winner) CheckersUI.Stop(); } // Get new game type NewGameDialog newGame = new NewGameDialog(settings, agentNames); // Set defaults newGame.GameType = gameType; newGame.Player1Name = lblNameP1.Text; newGame.Player2Name = lblNameP2.Text; // Show dialog if (newGame.ShowDialog(this) == DialogResult.Cancel) { return; } // Set new game parameters gameType = newGame.GameType; agent = null; // Set Game Panel properties lblNameP1.Text = newGame.Player1Name; lblNameP2.Text = newGame.Player2Name; picPawnP1.Image = newGame.ImageSet[0]; picPawnP2.Image = newGame.ImageSet[2]; // Set UI properties switch (gameType) { case CheckersGameType.SinglePlayer: CheckersUI.Player1Active = true; CheckersUI.Player2Active = false; agent = agents[newGame.AgentIndex]; break; case CheckersGameType.Multiplayer: CheckersUI.Player1Active = true; CheckersUI.Player2Active = true; break; case CheckersGameType.NetGame: remotePlayer = newGame.RemotePlayer; if (remotePlayer == null) { MessageBox.Show(this, "Remote user disconnected before the game began", "Checkers", MessageBoxButtons.OK, MessageBoxIcon.Exclamation); return; } CheckersUI.Player1Active = true; CheckersUI.Player2Active = (remotePlayer == null); if (!menuViewNetPanel.Checked) { menuViewNetPanel_Click(menuViewNetPanel, EventArgs.Empty); } tmrConnection.Start(); panNet.Visible = true; lnkLocalIP.Text = Dns.GetHostByName(Dns.GetHostName()).AddressList[0].ToString(); lnkRemoteIP.Text = ((IPEndPoint)remotePlayer.Socket.RemoteEndPoint).Address.ToString(); AppendMessage("", "Connected to player"); break; default: return; } CheckersUI.CustomPawn1 = newGame.ImageSet[0]; CheckersUI.CustomKing1 = newGame.ImageSet[1]; CheckersUI.CustomPawn2 = newGame.ImageSet[2]; CheckersUI.CustomKing2 = newGame.ImageSet[3]; // Create the new game CheckersGame game = new CheckersGame(); game.FirstMove = newGame.FirstMove; // Start a new checkers game CheckersUI.Play(game); }
/// <summary>Creates a duplicate Checkers piece object from an identical (possibly cloned) game.</summary> /// <returns>The new Checkers piece object.</returns> public CheckersPiece Clone(CheckersGame game) { CheckersPiece clonedPiece = game.PieceAt(Location); // Make sure piece exists if(clonedPiece == null) return null; // Make sure piece is equivalent if((clonedPiece.Player != Player) || (clonedPiece.InPlay != InPlay) || (clonedPiece.Rank != Rank)) return null; // Return cloned piece return clonedPiece; }
/// <summary>Creates a Checkers game from supplied game parameters.</summary> /// <param name="optionalJumping">The Optional Jumping rule.</param> /// <param name="board">The Checkers board that makes up the game.</param> /// <param name="turn">Whose turn it is.</param> /// <param name="winner">The winner, or 0 if none yet.</param> /// <returns>The Checkers game.</returns> public static CheckersGame Create(bool optionalJumping, CheckersPiece[,] board, int turn, int winner) { if((board.GetLength(0) != BoardSize.Width) || (board.GetLength(1) != BoardSize.Height)) throw new ArgumentOutOfRangeException("board", board, "Board's dimensions must be " + BoardSize.Width + "x" + BoardSize.Height); CheckersGame game = new CheckersGame(optionalJumping); game.board = new CheckersPiece[BoardSize.Width, BoardSize.Height]; game.pieces = new CheckersPieceCollection(); for(int y = 0; y < BoardSize.Height; y++) for(int x = 0; x < BoardSize.Width; x++) { CheckersPiece piece = board[x, y]; if(piece == null) continue; if(piece.Owner != null) throw new ArgumentOutOfRangeException("board", board, "Board contains a Checkers piece that belongs to another Checkers game."); if(!piece.InPlay) throw new ArgumentOutOfRangeException("board", board, "Board contains a Checkers piece that is not in play."); if((piece.Location.X != x) || (piece.Location.Y != y)) throw new ArgumentOutOfRangeException("board", board, "Board contains a Checkers piece that does not match up with it's board location."); if((piece.Player != 1) || (piece.Player != 2)) throw new ArgumentOutOfRangeException("board", board, "Board contains a Checkers piece that is not associated with a valid player."); game.pieces.Add(new CheckersPiece(game, piece.Player, piece.Rank, piece.Location, piece.InPlay)); } game.isPlaying = true; game.turn = turn; game.winner = winner; return game; }
/// <summary>Creates a duplicate Checkers move object from an identical (possibly cloned) game.</summary> /// <returns>The new Checkers move object.</returns> public CheckersMove Clone(CheckersGame game) { return(FromPath(game, piece.Clone(game), Path)); }
/// <summary>Creates a duplicate Checkers game object.</summary> /// <returns>The new Checkers move object.</returns> public CheckersGame Clone() { CheckersGame game = new CheckersGame(optionalJumping); game.isReadOnly = isReadOnly; game.isPlaying = isPlaying; game.firstMove = firstMove; game.turn = turn; game.winner = winner; game.pieces = new CheckersPieceCollection(); game.board = new CheckersPiece[BoardSize.Width, BoardSize.Height]; foreach(CheckersPiece piece in pieces) { CheckersPiece newPiece = new CheckersPiece(game, piece.Player, piece.Rank, piece.Location, piece.InPlay); game.board[newPiece.Location.X, newPiece.Location.Y] = newPiece; game.pieces.Add(newPiece); } int lastMovePieceIndex = ((lastMove != null) ? (pieces.IndexOf(lastMove.Piece)) : (-1)); game.lastMove = ((lastMovePieceIndex != -1) ? (CheckersMove.FromPath(game, game.pieces[lastMovePieceIndex], lastMove.Path)) : (null)); return game; }
public void kingMe(CheckersGame game, Square loc) { if (loc.squareNumber < 4) { if (loc.squareType == CheckerType.redChecker) loc.squareType = CheckerType.redKing; else return; } else if (loc.squareNumber >= 28) { if (loc.squareType == CheckerType.whiteChecker) loc.squareType = CheckerType.whiteKing; else return; } else return; }
/// <summary> /// Starts a new game. /// </summary> private void DoNewGame() { if((CheckersUI.IsPlaying) || (CheckersUI.Winner != 0)) { if(!DoCloseGame()) return; // Stop current game (with no winner) CheckersUI.Stop(); } // Get new game type NewGameDialog newGame = new NewGameDialog(settings, agentNames); // Set defaults newGame.GameType = gameType; newGame.Player1Name = lblNameP1.Text; newGame.Player2Name = lblNameP2.Text; // Show dialog if(newGame.ShowDialog(this) == DialogResult.Cancel) return; // Set new game parameters gameType = newGame.GameType; agent = null; // Set Game Panel properties lblNameP1.Text = newGame.Player1Name; lblNameP2.Text = newGame.Player2Name; picPawnP1.Image = newGame.ImageSet[0]; picPawnP2.Image = newGame.ImageSet[2]; // Set UI properties switch(gameType) { case CheckersGameType.SinglePlayer: CheckersUI.Player1Active = true; CheckersUI.Player2Active = false; agent = agents[newGame.AgentIndex]; break; case CheckersGameType.Multiplayer: CheckersUI.Player1Active = true; CheckersUI.Player2Active = true; break; case CheckersGameType.NetGame: remotePlayer = newGame.RemotePlayer; if(remotePlayer == null) { MessageBox.Show(this, "Remote user disconnected before the game began", "Checkers", MessageBoxButtons.OK, MessageBoxIcon.Exclamation); return; } CheckersUI.Player1Active = true; CheckersUI.Player2Active = (remotePlayer == null); if(!menuViewNetPanel.Checked) menuViewNetPanel_Click(menuViewNetPanel, EventArgs.Empty); tmrConnection.Start(); panNet.Visible = true; lnkLocalIP.Text = Dns.GetHostByName(Dns.GetHostName()).AddressList[0].ToString(); lnkRemoteIP.Text = ((IPEndPoint)remotePlayer.Socket.RemoteEndPoint).Address.ToString(); AppendMessage("", "Connected to player"); break; default: return; } CheckersUI.CustomPawn1 = newGame.ImageSet[0]; CheckersUI.CustomKing1 = newGame.ImageSet[1]; CheckersUI.CustomPawn2 = newGame.ImageSet[2]; CheckersUI.CustomKing2 = newGame.ImageSet[3]; // Create the new game CheckersGame game = new CheckersGame(); game.FirstMove = newGame.FirstMove; // Start a new checkers game CheckersUI.Play(game); }