I've been trying to build this game with a friend and so far he hasn't been much help so I am going to ask this community for suggestions and advice for building my Connect4 console game. So far I am filling the guts, I haven't put a method to check for 4 in a row yet and for both of the user's inputs. Also I don't know how to make my Move method start so you cant start dropping in the pieces.
I left a few notes in my comments on whether I should implement another class to store my row0,1,2,3,4,5,6 to see if my column is full or not.
I just want a more efficient ideas to create my code. I keep wanting to add more arguments in it!
If I should create another class could someone guide me on how I should do that with what I got, I'm not very familiar with gets, sets, values but it seems like I might need to use this?
I apologize if there's too much code here.
Much appreciated,
-Dustin
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace Connect4
{
class Program
{
static void Main(string[] args)
{
DisplayInstructions();
Play();
}
public static void Play()
{
char[,] board = new char[6, 7]; //Create board.
int rows = 5; //I dont know if I need these
int columns = 6; //and this
bool checkForFull = false; //Initialize method objects.
int rowCheck = 0;
Board(board, rows, columns); //Fills board with +'s.
PrintBoard(board, rows, columns, checkForFull, rowCheck); //Display's board
//char playerX = 'X';
//char playerO = 'O';
Move(board, rows, columns, checkForFull, rowCheck); //Start to play.
Console.ReadKey();
}
//Checks input for validation might need better way to check the input.
public static int PlayerTurn()
{
string inColumnNum;
int columnPicked;
inColumnNum = Console.ReadLine();
columnPicked = int.Parse(inColumnNum);
if (columnPicked >= 6 || columnPicked <= 1)
{
Console.WriteLine("Please pick the appropriate column");
//Move(playerX, playerO);
}
return columnPicked;
}
//Checks for full board if so then declares as true.
public static void FullBoard(char[,] board, int rows, int columns, bool checkForFull, int rowCheck)
{
int total = 0;
for (int i = 0; i < 6; i++)
{
for (int j = 0; j < 7; j++)
{
if (board[i, j] == 'X' || board[i, j] == 'O')
{
total += 1;
if (total == 42)
{
checkForFull = true;
PrintBoard(board, rows, columns, checkForFull, rowCheck);
Console.WriteLine("\nIt's a tie!");
PromptToPlayAgain();
}
}
}
}
checkForFull = false;
}
public static void ColumnDepthKeeper(int rowCheck)
{
if (rowCheck == 0)
{
//Probably going to need another method to keep track of column depth
}
}
public static char[,] Move(char[,] board, int rows, int columns, bool checkForFull, int rowCheck)
{
int row0 = 6;
int row1 = 6;
int row2 = 6;//Might have to go into another class to be stored becasue it will keep reseting if its placed here and cant be initialized outside the static class
int row3 = 6;//Unless you want 7 methods or if you know a more effecient way of doing it?
int row4 = 6;
int row5 = 6;
int row6 = 6;
PlayerTurn();//I dont know how to start the game?
while (checkForFull == false)
{
if (PlayerTurn() == 0) //Checks if player chooses column 1.
{
if (row0 == 0) //Checks if column 1 is full.
{
Console.WriteLine("This column is full please choose an empty slot.");
return Move(board, rows, columns, checkForFull, rowCheck);
}
else //If not, adds new peice.
{
board[row0, PlayerTurn()] = 'X'; //Sets picked column and bottom of row with X or O.
row0--; //Update the row stored... This needs to come from another classs somehow?
row0 = rowCheck; //Sends to method to check if full
ColumnDepthKeeper(rowCheck);
PrintBoard(board, rows, columns, checkForFull, rowCheck); //Show the board again.
return Move(board, rows, columns, checkForFull, rowCheck); //Run through new input.
}
}
else if (PlayerTurn() == 1)
{
if (row1 == 0)
{
Console.WriteLine("This column is full please choose an empty slot.");
return Move(board, rows, columns, checkForFull, rowCheck);
}
else
{
board[row1, PlayerTurn()] = 'X';
row1--;
row1 = rowCheck;
ColumnDepthKeeper(rowCheck);
PrintBoard(board, rows, columns, checkForFull, rowCheck);
return Move(board, rows, columns, checkForFull, rowCheck);
}
}
else if (PlayerTurn() == 2)
{
if (row2 == 0)
{
Console.WriteLine("This column is full please choose an empty slot.");
return Move(board, rows, columns, checkForFull, rowCheck);
}
else
{
board[row2, PlayerTurn()] = 'X';
row2--;
row2 = rowCheck;
ColumnDepthKeeper(rowCheck);
PrintBoard(board, rows, columns, checkForFull, rowCheck);
return Move(board, rows, columns, checkForFull, rowCheck);
}
}
else if (PlayerTurn() == 3)
{
if (row3 == 0)
{
Console.WriteLine("This column is full please choose an empty slot.");
return Move(board, rows, columns, checkForFull, rowCheck);
}
else
{
board[row3, PlayerTurn()] = 'X';
row3--;
row3 = rowCheck;
ColumnDepthKeeper(rowCheck);
PrintBoard(board, rows, columns, checkForFull, rowCheck);
return Move(board, rows, columns, checkForFull, rowCheck);
}
}
else if (PlayerTurn() == 4)
{
if (row4 == 0)
{
Console.WriteLine("This column is full please choose an empty slot.");
return Move(board, rows, columns, checkForFull, rowCheck);
}
else
{
board[row4, PlayerTurn()] = 'X';
row4--;
row4 = rowCheck;
ColumnDepthKeeper(rowCheck);
PrintBoard(board, rows, columns, checkForFull, rowCheck);
return Move(board, rows, columns, checkForFull, rowCheck);
}
}
else if (PlayerTurn() == 5)
{
if (row5 == 0)
{
Console.WriteLine("This column is full please choose an empty slot.");
return Move(board, rows, columns, checkForFull, rowCheck);
}
else
{
board[row4, PlayerTurn()] = 'X';
row5--;
row5 = rowCheck;
ColumnDepthKeeper(rowCheck);
PrintBoard(board, rows, columns, checkForFull, rowCheck);
return Move(board, rows, columns, checkForFull, rowCheck);
}
}
else if (PlayerTurn() == 6)
{
if (row6 == 0)
{
Console.WriteLine("This column is full please choose an empty slot.");
return Move(board, rows, columns, checkForFull, rowCheck);
}
else
{
board[row6, PlayerTurn()] = 'X';
row6--;
row6 = rowCheck;
ColumnDepthKeeper(rowCheck);
PrintBoard(board, rows, columns, checkForFull, rowCheck);
return Move(board, rows, columns, checkForFull, rowCheck);
}
}
return Move(board, rows, columns, checkForFull, rowCheck);
}
Console.WriteLine("Game Over.");
PromptToPlayAgain(); //When the board is full and check for full = true then ask to play again.
return Move(board, rows, columns, checkForFull, rowCheck); //Need this just cause.
}
//Fills the empty array.
public static void Board(char[,] board, int rows, int columns)
{
for (int i = 0; i < 6; i++)
{
for (int z = 0; z < 7; z++)
{
board[i, z] = '+';
}
}
}
//Will print the board when called.
public static void PrintBoard(char[,] board, int rows, int columns, bool checkForFull, int rowCheck)
{
{
Console.WriteLine("Current Board (user1 = O, user2 = X): \n");
for (int i = 0; i < 6; i++)
{
for (int j = 0; j < 7; j++)
{
Console.Write(board[i, j]);
}
Console.WriteLine();
}
}
}
//Will ask if your done player when called.
public static void PromptToPlayAgain()
{
DialogResult Result = MessageBox.Show("Lets play again!",
"Message", MessageBoxButtons.YesNo,
MessageBoxIcon.Exclamation);
if (Result == DialogResult.Yes)
{
Play();
}
else
{
EndGame();
}
}
//Ends the game.
public static void EndGame()
{
Console.WriteLine("You chose to end the game.\nThis game is going to close in 4 seconds...");
System.Threading.Thread.Sleep(4000);
System.Environment.Exit(0);
}
//Displays the instructions once.
public static void DisplayInstructions()
{
Console.WriteLine("I'm glad you've chosen to play Connect 4!\nTo play enter the row player X or player O wants to put his/hers piece.");
}
}
}