Skip to content

Instantly share code, notes, and snippets.

@MaxHasADHD
Created September 22, 2015 01:13
Show Gist options
  • Save MaxHasADHD/cb1f77346a605725a30d to your computer and use it in GitHub Desktop.
Save MaxHasADHD/cb1f77346a605725a30d to your computer and use it in GitHub Desktop.
#include <iostream>
#include <ctime>
using namespace std;
// Methods
void constructBoard();
void checkWinner();
// Global
string moves[9] = { " ", " ", " ", " ", " ", " ", " ", " ", " " };
int main() {
// Variables
bool gameOver = 0;
bool gameWon = 0;
// Instructions
cout << "Welcome to Tic-Tac-Toe!" << endl;
cout << "Rules:" << endl;
cout << "When it is your turn, press numbers 1-9 to select a spot." << endl << "Row one is numbers 1-3, Row two is numbers 4-6" << endl << "row three is numbers 7-9" << endl;
cout << "It is your turn..." << endl;
constructBoard();
while (gameOver == 0) {
// Variables
int placement = 0;
// Logic
cout << "Enter a number 1-9.." << endl;
cin >> placement;
if (placement > 0 && placement < 10) {
moves[placement-1] = "X";
constructBoard();
checkWinner();
}
else {
cout << "That was not a valid number, please try again" << endl;
}
}
return 0;
}
void constructBoard() {
int column;
for (column = 0; column < 3; column++) {
int row;
for (row = 0; row < 3; row++) {
int num = (column * 3)+row;
string move = moves[num];
cout << move << " | ";
}
cout << endl;
}
}
void checkWinner() {
// Patters for a win
// 0, 1, 2
// 3, 4, 5
// 6, 7, 8
// 0, 3, 6
// 1, 4, 7
// 2, 5, 8
// 0, 4, 8
// 2, 4, 6
// Get indexs
int * indexes = new int [9];
int currentIndex = 0;
int column;
for (column = 0; column < 3; column++) {
int row;
for (row = 0; row < 3; row++) {
int num = (column * 3)+row;
indexes[currentIndex] = num;
currentIndex++;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment