connect4

Started by
3 comments, last by Bincho 18 years, 11 months ago
I'm currently working on a connect4 game as my second game and am starting to get the hang of the slightly different mindset used for this. A couple of questions though. But first here's a look at the current source. Please don't add to the source... I kinda want to figure it out myself.

#include <iostream>
#include <iomanip>
using namespace std;

const char PLAYER1 = 'R';
const char PLAYER2 = 'Y';
const int MAXROWS = 6;
const int MAXCOLUMNS = 7;
const int MAXTURNS = MAXROWS * MAXCOLUMNS;
const char player[] = {PLAYER1, PLAYER2};

char board[maxRows][maxColumns];

void resetBoard()
{
   for(int i = 0; i < MAXROWS; ++i)
   {
      for(int j = 0; j < maxColumns; ++j)
      {
         board[j] = ' ';
      }
   }
}

void displayBoard()
{
   for(int i = 0; i < MAXROWS; ++i)
   {
      for(int j = 0; j < MAXCOLUMNS; ++j)
      {
         cout << board[j];
      }
      cout << endl;
   }
   for(int i = 0; i < MAXCOLUMNS; ++i)
   {
      cout << "-";
   }
   cout << endl;
   for(int i = 0; i < MAXCOLUMNS; ++i)
   {
      cout << i + 1;
   }
   cout << endl;
}



void getInput(int &row, int &col)
{
   int move;
   while(true)
   {
      cout <<
   }
}

int main()
{
   resetBoard();
   int turn = 0;
   int rowChoice, colChoice;
   int winner = 0;
   while(turn < MAXTURNS)
   {
      displayBoard();
      getInput(rowChoice, colChoice);
      board[rowChoice][colChoice] = player[turn%2];
      if(winner = checkWin(rowChoice, colChoice))
      {
         break;
      }
   }
   switch(winner)
   {
      case PLAYER1:
         cout << "Player 1 has won the game." << endl;
         break;
      case PLAYER2:
         cout << "Player 2 has won the game." << endl;
         break;
      default:
         cout << "The game ended in a tie." << endl;
         break;
   }
   return 0;
}

Obviously, I still have a lot of work left to do on it. My main question, is what would be better for checking the win condition. I've thought about either making it check through all possible combinations...(brute force approach) or putting the last move made into the function, and checking in the eight directions around it. I want to get this game going well... since it will be one of my first 2D games to do. I'll port most of the code. I understand that the brute-force approach is highly doable with the scale of a board i am using... however if i increase it... the problem grows worse... whereas it doesn't with the other approach. Thoughts and opinions are welcome. But please don't post code, until I actually put it up for scrutiny.
Advertisement
If you're doing this to practice programming, I'd say both. The reason I said this, is because I probably would've gone for brute-forcing. But I find the more systemated solution extremely interesting.
You have the right idea about checking the winning condition (the non-brute force one that is). I was going to say something about the input method, but since it's basically empty, I'm going to leave that until you put some code into it. ;)
The input method is going to be setup so that they pick a column to put the piece into and then it tests to see if that's a valid move. If it is a valid move it assigns the passed by reference values to the column and the first vaild row. I'm about to invert the board display so [0][0] is the bottom left. Any other suggestions(not code) is welcome.
If I was going to brute force it I would probably end up designing an algorithm that took any specific location on the board and then searched for any and all solutions that involved said specific location. If I was being more conservative I might only use the specific location as an endpoint to the four in a row to ignore overlap. But if I used the former, then it would be pretty much the same algorithm you'd use to find if the most recent play caused a win condition. I don't know if this helps at all, but there you go.

This topic is closed to new replies.

Advertisement