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 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200
|
#include <iostream>;
using namespace std;
int board [3][3];
// a multidimensional array for the 3x3 gameboard.
int input1;
// player 1's input
int input2;
// player 2's input
int y = 0;
// for use in the printgame function. Used for scanning the y-axis.
int x = 0;
// for use in the printgame function. Used for scanning the x-axis.
int turn = 0;
// turn number
bool oper = true;
// for use with a future victory checking function (currently not programmed)
/* Here's the deal: I want to build a viable framework for a tic-tac-toe program.
This incomplete build was solely designed to take player inputs and display them
correctly on the gameboard; the other players marks would be easily overwritten,
and there would be no victory checking. However I'm having problems even at this
stage. */
/* ERROR LOG FROM WINDOWS VISUAL C++ 2008 EXPRESS EDITION
1>z:\students\wmeine1890\visual studio 2008\projects\t3\t3\game.cpp(44) : error C2087: 'defboard' : missing subscript
1>z:\students\wmeine1890\visual studio 2008\projects\t3\t3\game.cpp(180) : error C2664: 'printgame' : cannot convert parameter 1 from 'int [3][3]' to 'int [][1]'
1> Types pointed to are unrelated; conversion requires reinterpret_cast, C-style cast or function-style cast
1>z:\students\wmeine1890\visual studio 2008\projects\t3\t3\game.cpp(189) : error C2664: 'printgame' : cannot convert parameter 1 from 'int [3][3]' to 'int [][1]'
1> Types pointed to are unrelated; conversion requires reinterpret_cast, C-style cast or function-style cast
1>z:\students\wmeine1890\visual studio 2008\projects\t3\t3\game.cpp(195) : error C2664: 'printgame' : cannot convert parameter 1 from 'int [3][3]' to 'int [][1]'
1> Types pointed to are unrelated; conversion requires reinterpret_cast, C-style cast or function-style cast
*/
/* I'm confused as to what's wrong exactly, as I can detect no basic errors;
it must be a couple of specific details that I've messed up. I will add
comments as I go to detail my thoughts and explain the program structure
as clearly as I can. */
void printgame (int defboard[][], int width, int height)
// prints the current gameboard
{
if (turn == 0)
cout << "INITIAL GAMEBOARD\n";
// Only displays the first time it prints
else
cout << "GAMEBOARD FOR TURN " << turn << ":\n";
do {
switch (defboard[y][x]) // Scan for values in a space of the array. This space changes as the function progresses.
/* My intuition says that the problem is here; that for some reason,
the program is interpreting this as a new array definition rather than
a reference to an existing array value. */
{
case 1:
/* Player 1 marks spaces of the array with the number 1.
See the "mark" functions below. */
cout << "[X] "; // Player 1's mark is 1, therefore it denotes an X
if (x == width - 1)
/* With "defboard[y][x]" I am accessing values of the array.
Therefore the width/height values are shifted by one. */
cout << "\n";
/* If the x-axis scan reaches the end of a "row", it creats a newline.
This prevents the gameboard from beign displayed all in one row. */
break;
case 2:
cout << "[O] "; // Player 2's mark is 2, outputting as an O.
if (x == width - 1) // ditto above.
cout << "\n";
break;
default:
cout << "[ ] "; // If it can't find a mark, it outputs a blank space.
if (x == width - 1)
cout << "\n";
}
if (x == 0 || x < width)
// If x is 0 (the first x value of the array) OR x is less than the width...
{
x++;
// ...go to the next x value in the row.
}
if (x == width && y < height)
//If the x value is at the end of a row AND there are unchecked rows remaining...
{
x = 0; // ...go to the first x value...
y++; // ...in the next row.
}
} while (x =! width, y =! height);
// all while the x and y value haven't exceeded the limits of the board.
turn++; // increases the turn count after every move
x = 0;
y = 0;
/* resetting x and y. I did this so that the program wouldn't start from the last
space it's supposed to check...unnecessary? */
}
void player1 (int p1mark) // Player 1's mark on the board.
{
switch (p1mark)
{
case 1:
/* See main function below: Each integer value here represents a space
the player chooses to mark. */
board [0][0] = 1; // defines corresponding array value.
break;
case 2:
board [0][1] = 1;
break;
case 3:
board [0][2] = 1;
break;
case 4:
board [1][0] = 1;
break;
case 5:
board [1][1] = 1;
break;
case 6:
board [1][2] = 1;
break;
case 7:
board [2][0] = 1;
break;
case 8:
board [2][1] = 1;
break;
case 9:
board [2][2] = 1;
break;
default:
cerr << "Invalid entry"; // does not tolerate invalid values
break;
}
}
void player2 (int p2mark) // ditto above for player 2
{
switch (p2mark)
{
case 1:
board [0][0] = 2;
break;
case 2:
board [0][1] = 2;
break;
case 3:
board [0][2] = 2;
break;
case 4:
board [1][0] = 2;
break;
case 5:
board [1][1] = 2;
break;
case 6:
board [1][2] = 2;
break;
case 7:
board [2][0] = 2;
break;
case 8:
board [2][1] = 2;
break;
case 9:
board [2][2] = 2;
break;
default:
cerr << "Invalid entry";
break;
}
}
int main ()
{
cout << "##########TIC-TAC-TOE##########\n"; // just some info text.
cout << "* Player 1 is X. *\n* Player 2 is O. *\n";
printgame (board, 3, 3); // prints the initial blank gameboard.
while(oper == true)
{
cout << "\nPlayer 1, please mark a square by inputting the corresponding number:";
cout << "\n[1] [2] [3]\n[4] [5] [6]\n[7] [8] [9]\n";
cin >> input1;
// Player 1 inputs the number corresponding to the space he wishes to mark.
player1 (input1);
// Puts Player 1's mark on the board.
printgame (board, 3, 3);
// Prints the newly marked gameboard.
cout << "\nPlayer 2, please mark a square by inputting the corresponding number:"; // Repeats the process for player 2.
cout << "\n[1] [2] [3]\n[4] [5] [6]\n[7] [8] [9]\n";
cin >> input2;
player2 (input2);
printgame (board, 3, 3);
} /* As stated, the boolean operand would keep the loop going until a victory
condition had been met. This functionality has not been implemented. */
return 0;
/* That's all, folks! */
}
|