using System; namespace Lecture_11_console { // ------------------------------------------------------------------- // ------------------------------------------------------------------- public class Connect4Board { private char[,] state; private int boardHeight; private int boardWidth; public int BoardHeight { get { return boardHeight; } set { if ((value >= 4) && (value <= 26)) boardHeight = value; else boardHeight = 6; } } public int BoardWidth { get { return boardWidth; } set { if ((value >= 4) && (value <= 26)) boardWidth = value; else boardWidth = 7; } } // ------------------------------------------------------------------- public Connect4Board() { Setup(7,6); } // ------------------------------------------------------------------- public Connect4Board(int width, int height) { Setup(width, height); } // ------------------------------------------------------------------- private void Setup(int width, int height) { BoardWidth = width; BoardHeight = height; state = new char[width,height]; ClearBoard(); } // ------------------------------------------------------------------- public void ClearBoard() { int i, j; for(i=0; i < BoardWidth; i++) for(j=0; j < BoardHeight; j++) state[i,j] = ' '; } // ------------------------------------------------------------------- public int Drop(char player_mark, int column) { // check to see if the column is valid (on the board) if ((column >= BoardWidth) || (column < 0)) return -2; int i; i = BoardHeight - 1; while ((i >= 0) && (state[column,i] == ' ')) { i--; } i++; // return to the last non-empty row // check to see if the column is full if (i == BoardHeight) return -1; // mark the piece state[column,i] = player_mark; return i; // return the row of the newly placed piece } // ------------------------------------------------------------------- public int CheckWin(char player_mark, int column, int row) { int i, j; int count, i_stop, j_stop; // Console.WriteLine("Checking for: " + player_mark + "( " + column + " , " + row + " )"); // check to make sure the position to check is valid if ((column < 0) || (column >= BoardWidth) || (row < 0) || (row >= BoardHeight)) return -1; count = 0; i = Math.Max(0, column - 3); i_stop = Math.Min(BoardWidth - 1, column + 3); while (i <= i_stop) { if (state[i, row] == player_mark) { count++; if (count == 4) return 1; } else { count = 0; } i++; } count = 0; j = Math.Max(0, row - 3); j_stop = Math.Min(BoardHeight - 1, row + 3); while (j <= j_stop) { if (state[column, j] == player_mark) { count++; if (count == 4) return 1; } else { count = 0; } j++; } count = 0; i = Math.Max(0, column - 3); i_stop = Math.Min(BoardWidth - 1, column + 3); j = Math.Max(0, row - 3); j_stop = Math.Min(BoardHeight - 1, row + 3); while ((i <= i_stop) && (j <= j_stop)) { if (state[i, j] == player_mark) { count++; if (count == 4) return 1; } else { count = 0; } i++; j++; } count = 0; i = Math.Max(0, column - 3); i_stop = Math.Min(BoardWidth - 1, column + 3); j = Math.Min(BoardHeight - 1, row + 3); j_stop = Math.Max(0, row - 3); while ((i <= i_stop) && (j >= j_stop)) { if (state[i, j] == player_mark) { count++; if (count == 4) return 1; } else { count = 0; } i++; j--; } return 0; } // ------------------------------------------------------------------- public override string ToString() { int i, j; string rv = ""; // for(i=0; i < BoardHeight; i++) for(i=BoardHeight-1; i >= 0; i--) { for(j=0; j < BoardWidth; j++) rv += state[j, i] + " "; rv += "\n"; } return rv; } } // ------------------------------------------------------------------- // ------------------------------------------------------------------- public class Connect4Game { private int currentPlayer; private char[] playerMarks; public int CurrentPlayer { get { return currentPlayer; } set { if ((value == 0) || (value == 1)) currentPlayer = value; } } private int gameComplete; public int GameComplete { get { return gameComplete; } } private int moveCount; public int MoveCount { get { return moveCount; } } public Connect4Board Board; // ------------------------------------------------------------------- public Connect4Game () { Setup(7,6); } // ------------------------------------------------------------------- public Connect4Game (int w, int h) { Setup(w,h); } // ------------------------------------------------------------------- private void Setup(int w, int h) { Board = new Connect4Board(w,h); playerMarks = new char[2] {'X', 'O'}; Reset(); } // ------------------------------------------------------------------- public void Reset() { gameComplete = -1; // open game moveCount = 0; // no moves made yet Board.ClearBoard(); CurrentPlayer = 0; } // ------------------------------------------------------------------- public int TakeTurn(int column) { int rv, win; rv = Board.Drop(playerMarks[CurrentPlayer], column); if (rv < 0) return rv; moveCount++; win = Board.CheckWin(playerMarks[CurrentPlayer], column, rv); if (win == 1) gameComplete = CurrentPlayer+1; // add one to denote which player won - 1 or 2 else if (MoveCount == Board.BoardHeight * Board.BoardWidth) gameComplete = 0; // tie else CurrentPlayer = (CurrentPlayer + 1) % 2; // swap current player to next player return rv; } } // ------------------------------------------------------------------- // ------------------------------------------------------------------- public class C4 { static private string[] playerNames; static private Connect4Game c4G; static private int[] playerWins; // ------------------------------------------------------------------- static private void GetNames() { playerNames = new string[2]; Console.Write("Player 1, please enter your name: "); playerNames[0] = Console.ReadLine(); Console.Write("Player 2, please enter your name: "); playerNames[1] = Console.ReadLine(); } // ------------------------------------------------------------------- static private void GetBoardSize(out int width, out int height) { do { Console.Write("How wide should the board be (4-26): "); width = Int32.Parse(Console.ReadLine()); } while ((width < 4) || (width > 26)); do { Console.Write("How tall should the board be (4-26): "); height = Int32.Parse(Console.ReadLine()); } while ((height < 4) || (height > 26)); } // ------------------------------------------------------------------- static private void Setup() { int w, h; GetNames(); playerWins = new int[3] {0, 0, 0}; GetBoardSize(out w, out h); c4G = new Connect4Game(w, h); } // ------------------------------------------------------------------- static private bool GetContinue() { string r; do { Console.Write("Play again (y,n)? "); r = Console.ReadLine(); r = r.ToLower(); } while ((r[0] != 'y') && (r[0] != 'n')); return (r[0] == 'y'); } // ------------------------------------------------------------------- static private void ShowWins() { Console.WriteLine(playerNames[0] + " has won " + playerWins[1] + " game(s)."); Console.WriteLine(playerNames[1] + " has won " + playerWins[2] + " game(s)."); Console.WriteLine(playerWins[0] + " game(s) resulted in a tie."); } // ------------------------------------------------------------------- static public int GetColumn() { int rv; do { Console.Write("Enter column to drop (0-" + (c4G.Board.BoardWidth - 1) + ") "); rv = Int32.Parse(Console.ReadLine()); } while ((rv < 0) || (rv >= c4G.Board.BoardWidth)); return rv; } // ------------------------------------------------------------------- static public void Play() { bool GameOver = false; int column; c4G.Reset(); while (! GameOver) { Console.WriteLine(c4G.Board.ToString()); column = GetColumn(); c4G.TakeTurn(column); GameOver = (c4G.GameComplete >= 0); } Console.WriteLine(c4G.Board.ToString()); playerWins[c4G.GameComplete]++; } // ------------------------------------------------------------------- [STAThread] static void Main(string[] args) { bool cont = true; Setup(); while (cont) { Play(); ShowWins(); cont = GetContinue(); } } } }