connect 5 game

I'm trying to make a program that will play a "connect 5" game for school. The game is between two players, and a player wins if they match five in a row horizontally, vertically, or diagonally. There cannot be any "magic numbers".

I have most of it finished but I am having trouble getting the users inputs to write into the array. I have tried plugging it in several different ways but it always gives me an error. Right now it says "illegal instruction" when I run it after inputting the location and letter. I also haven't done the part to check for a winner in the diagonal direction yet, since I have been trying to check if the inputs were being written correctly first.

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
#include <iostream>
  #include <cstdlib>
  #include <ctime>
  #include <cctype>
  using namespace std;

  //Size of the board
  const int MAX_SIZE = 5;
  const char EMPTY = '.';
 

   void print_array (char arr[][MAX_SIZE]) {
      for (int i = 0; i < MAX_SIZE; i++) {
          for (int j = 0; j < MAX_SIZE; j++) {
              //YOU: Print out the array values here
              cout << arr[i][j];
          }
          cout << endl;
      }
  }

  //Initializes the array to make it ready for a new game
  void clear_array (char arr[][MAX_SIZE]) {
      for (int i = 0; i < MAX_SIZE; i++) {
          for (int j = 0; j < MAX_SIZE; j++) {
              arr[i][j] = EMPTY;
          }
      }
  }

  //Will examine a board to see if there's a winning position
  //A winning position is a horizontal row or vertical column all with the same letter
  // in it. Letter '.' means an empty space, so don't win if we have five .'s in a row.
  //Return true if there's a winner, false otherwise.
  //Write the winning character into the winner parameter
  //If the board is invalid (two+ winners, etc.) set error to true, otherwise false
  bool check_winner (char arr[][MAX_SIZE], char &winner, bool &error) {
      for (int i = 0, count = 0; i < MAX_SIZE; i++) { //checks horizontal
          for (int j = 0; j < MAX_SIZE; j++) {
              if (arr[i][j] == arr[i][j + 1] && arr[i][j] != '.') {
                  count++;
                  if (count == MAX_SIZE) {
                      return true;
                  }
              }
          }
      }
      for (int i = 0, count = 0; i < MAX_SIZE; i++) { //checks vertical
          for (int j = 0; j < MAX_SIZE; j++) {
              if (arr[j][i] == arr[j + 1][i] && arr[j][i] != '.') {
                  count ++;
                  if (count == MAX_SIZE) {
                      return true;
                  }
              }
          }
      }


>>}
  void waiter (string error = "") {
      cout << error << "Press any key to continue.\n";
      string nothing;
      cin >> nothing;
  }

  int main() {
      char my_array[MAX_SIZE][MAX_SIZE] = {}; //= {} means initialize to zero
      srand (time (NULL)); //Initialize the random number generator

      //Start with empty board
      clear_array (my_array);

      while (true) {
          system ("clear"); //Clear the screen
          print_array (my_array);

          //Handle input and check for errors
          cout << "Input x and y coordinates followed by a letter (letter 'Q' to quit): ";
          int x, y;
          char input;
          cin >> x;
          if (!cin) exit (EXIT_FAILURE);
                                                cin >> y;
          if (!cin) exit (EXIT_FAILURE);
          cin >> input;
          if (!cin) exit (EXIT_FAILURE);
          if (input == 'Q') exit (EXIT_SUCCESS);
          if (!isalpha (input)) exit (EXIT_FAILURE);

          //Check to see if the coordinate is out of bounds
          if (false) {
              if (MAX_SIZE < x || MAX_SIZE < y)
                  return true;

              waiter ("That coordinate is out of bounds. "); //Pause
              continue; //Let them reenter the move
          }

         // Check for the spot being filled already
          if (false) {
              if (my_array[x][y] == '.')
                  waiter ("That spot is already taken. "); //Pause
              continue; //Let them reenter the move
          } else {
              // Enter the move onto the board here (this is what I'm stuck on, I'm not sure how to write in the letter of the char into the inputted location.
              my_array[x][y] = input;
          }

         char winner;
          bool error;
          if (check_winner (my_array, winner, error)) {
              //Print win message and/or error message here
              system ("clear"); //Clear the screen
              print_array (my_array);
              if (error) cout << "Error: Multiple winners!\n";
              else {
                  if (winner == AI)
                      cout << "Failure! The AI has beaten you!\n";
                  else
                      cout << "Congrats! Player " << winner << " has won!\n";
                  exit (EXIT_SUCCESS);
              }
              waiter(); //Pause
    }
      }
  }
               
Last edited on
Your posted code does not compile.

Line 5: You're missing the required <string> header.

Line 60: What is this?

Line 118: AI is not defined.

Logic error:

Lines 37-60: You never set the winner argument.
Last edited on
Topic archived. No new replies allowed.