Connect 4 project..complicated!

Hello, I am to do a connect 4 program and have the main part done, which I am not to change (instructed by my professor). However, I need help with a statement saying that all of the columns are full and to please choose another. Also, I would like help on the verticlewin function. The function needs to return 1 if someone wins and 0 if someone does not. I can not figure this out and have been working for weeks on this project! PLEASE help!!! I need it done tonight! Thanks in advance! Here's my program so far:


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
#include <iostream>
#include <fstream>

using namespace std;

void drawboard(ostream &, char [][7], int, int);
int humanmove(int player, char board [][7], int rows, int columns);
int verticalwin(char board[][7], int rows, int columns, int moverow, int movecolumn);

int main () {

  ofstream outfile;
  outfile.open("gamelog01.txt");
	
  char board[6][7] = {0};
	
  //board[0][0] = 'O';
  //board[2][4] = 'X';
	
  // WRITE YOUR MENU PART HERE!!!
  
  int player;
  int row;
  int col;
  int times;
  bool win; 	
  int moverow;
  int movecolumn;
  
  //cout << "How many players (up to 2): " ;
  //cin >> players;
  
  drawboard(cout, board,6,7);
  
 while (player = 1){
  humanmove(player, board, 6, 7);
  verticalwin(board, 6, 7, moverow, movecolumn);
  
  
  player = 2;
  humanmove(player, board, 6, 7);
  verticalwin(board, 6, 7, moverow, movecolumn);
}
    //}
	//while (times <= 42) {
  // This is a sample use of the drawboard function.
  // It prints out the board to the screen.
  // The second drawboard command prints the game board
  // to the output file called gamelog01.txt
  //drawboard(cout, board,6,7);
  //drawboard(outfile, board,6,7);

  outfile.close();
  return 0;
} // end main

// This function prints the board to the indicated ostream object.
// The values in the board are 'X' for player 1, 'O' (the letter) for
// player 2, and the ASCII value of 0 (zero) for an empty space.
void drawboard(ostream & out, char b[][7], int r, int c) {
  out << endl;
  for (int x = r-1; x >= 0; x--) {
    out << "  |";
    for (int y = 0; y < c-1; y++)
      out << "----";
    out << "---|" << endl << x << " |";
    for (int y = 0; y < c-1; y++)
      if (b[x][y] == 0)
	out << "   |";
      else
	out << " " << b[x][y] << " |";
    if (b[x][c-1] == 0)
      out << "   |" << endl;
    else
      out << " " << b[x][c-1] << " |" << endl;
  }
	
  out << "  |";
  for (int y = 0; y < c-1; y++)
    out << "----";
  out << "---|" << endl; 
  
  out << " ";
  for (int y = 0; y < c; y++)
    out << "   " << y;
    
  out << endl;
} // end drawboard function

int humanmove(int player, char board[][7], int rows, int columns){
 
  char symbol;

  if(player == 1)
    symbol = 'X';
  else
    symbol = 'O';

  int col;	
  cout << "What column would Player " << player << " like to play in?: " ;
  cin >> col;	
	
  while ((col < 0) || (col >= columns)){
    cout << "Invalid selection. There aren't that many columns. Please enter a different column number: ";
    cin >> col;
  }	
 
  int row;
  for(row = 5; row >= 0; row--){ 
    if(board[row][col] != 0)
      break;
  }  

  board[row+1][col] = symbol;
  if (row == 'X' || row == 'O'){
		cout << "Column " << col << " is full. Please choose another: ";
		cin >> col;
	}else{
	drawboard(cout, board, 6, 7);
	}
 } 
  
int verticalwin(char board[][7], int rows, int columns, int moverow, int movecolumn){




	
	
	
}




Last edited on
Code brackets silly [/code/] code here [//code /] remove all '/' once
Sorry! I resubmitted.
To check for wins, why not just check all directions when a player drops a piece and check if that player won?
Well I am trying to do something like that, just one piece at a time but I am unsure how to do so.
I made a connect four AI agent so of course I've made the game too, and I took the same approach that packetpirate suggested, checking in all directions from the last counter, as you'll only ever need to check whether the game is over after another counter has been entered. I won't post my exact source code but something like

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
if ( no moves have been taken )
    return false;

    int lastRow = moveStack_.top().row_;
    int lastCol = moveStack_.top().column_;
    // lastRow and lastCol store the position of the last move, as row and col below change and need to be reset to the original values before checking other directions

    int row = lastRow;
    int col = lastCol;
    int playerTurn = moveStack_.top().playerTurn_; // gets 'colour' of last move, represented by an int (1 or 2) in my program

    int left=0,right=0;

    while ( col >= 0 && BoardAt(row,col) == playerTurn ) // keep moving left until you find a counter which isn't the same colour, keep count of how many times you've moved left
    {
        left++;
	col--;
    }


    col = lastCol; // reset the column

    while (col < NUM_COLS && BoardAt(row,col) == playerTurn ) // same check as above but moving right
    {
	right++;
	col++;
    }

    if (left + right >= 5) // if enough counter in a line were found, the game is over
	return true;


this only checks for a horizontal win of course and you'll need to check vertical and diagonal wins in the same way, I'll leave that to you to figure out. Also don't just copy and paste this for your horizontal check as it may have faults, I haven't checked it properly, but it might give you an idea how to approach the problem.
Last edited on
thanks for your input!
Topic archived. No new replies allowed.