Afternoon Gents, I've been using this website for reference ever since I began C++ 4 months back. While I was coding for a tic tac toe game I ran into somewhat of a redundancy problem that hopefully you will be able to assist me with.
I've set up a game using an array that the "users" input the X's and O's. I run into problems when creating the METHOD of verifying who won. My current level of knowledge is using Loops, Array, and Functions.
The code so far:
#include <iostream>
using namespace std;
const int COLUMN_SIZE = 3, ROW_SIZE = 3;
char letter1 = 'X', letter2 = 'O';
int winner = 0;
-----------
| X | X | ♦ |
| ♦ | ♦ | O |
| ♦ | ♦ | ♦ |
-----------
Player2. Place your O. Row/Column*/
END CODE.
Is it absolutely necessary to use a combination of loops and if statements, to add up a line of indices to match the literal value ( X or O ) * 3 to find the winner?
is probably the most efficient way. if the expression is true, then the player whose token
is in any of the three positions is the winner.
You have to repeat the above checks for all 8 ways to win.
Alternatively, I would put all the index triples in an array and walk the array, checking each one.
Then you only have to write the above line once instead of 8 times.