Sign in to follow this  

connect4

This topic is 4595 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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[i][j] = ' ';
      }
   }
}

void displayBoard()
{
   for(int i = 0; i < MAXROWS; ++i)
   {
      for(int j = 0; j < MAXCOLUMNS; ++j)
      {
         cout << board[i][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.

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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. ;)

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites

This topic is 4595 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this