Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <windows.h>
- using namespace std;
- char n[10] = {'0', '1', '2', '3', '4', '5', '6', '7', '8', '9', }; // numbers on the game board
- char player = 'X'; // player symbol
- int win = 0;
- int ms[10] = {0,2,7,6,9,5,1,4,3,8}; //magic square template
- int wins[10]; // magic square to work out win
- void debug()
- {
- cout << ms[1] << ms[2] << ms[3] << endl;
- cout << ms[4] << ms[5] << ms[6] << endl;
- cout << ms[7] << ms[8] << ms[9] << endl;
- cout << wins[1] << wins[2] << wins[3] << endl;
- cout << wins[4] << wins[5] << wins[6] << endl;
- cout << wins[7] << wins[8] << wins[9] << endl;
- }
- void checkwin()
- {
- if(
- (wins[1] + wins[2] + wins[3] == 15) ||
- (wins[4] + wins[5] + wins[6] == 15) ||
- (wins[7] + wins[8] + wins[9] == 15) ||
- (wins[1] + wins[4] + wins[7] == 15) ||
- (wins[2] + wins[5] + wins[8] == 15) ||
- (wins[3] + wins[6] + wins[9] == 15) ||
- (wins[1] + wins[5] + wins[9] == 15) ||
- (wins[3] + wins[5] + wins[7] == 15))
- {
- win = 1;
- }
- else if(
- (wins[1] + wins[2] + wins[3] == -15) ||
- (wins[4] + wins[5] + wins[6] == -15) ||
- (wins[7] + wins[8] + wins[9] == -15) ||
- (wins[1] + wins[4] + wins[7] == -15) ||
- (wins[2] + wins[5] + wins[8] == -15) ||
- (wins[3] + wins[6] + wins[9] == -15) ||
- (wins[1] + wins[5] + wins[9] == -15) ||
- (wins[3] + wins[5] + wins[7] == -15))
- {
- win = 1; // will change this later for score
- }
- }
- void turn()
- {
- if (player == 'X')
- {
- player = 'O';
- }
- else
- {
- player = 'X';
- }
- }
- void choice()
- {
- int a;
- cout << "Enter a number:" << endl;
- cin >> a;
- if( isdigit(n[a]) == 1 ) // checks to see if square is already taken
- {
- if((a > 0) && (a < 10)) // checks to see if valid choice
- {
- n[a] = player;
- if(player == 'X')
- {
- wins[a] = wins[a] + ms[a]; // if winning magic square tiles add up to +15, X wins
- }
- else
- {
- wins[a] = wins[a] - ms[a]; // if they add up to -15 O wins.
- }
- }
- turn();
- }
- else
- {
- cout << "try again" << endl;
- Sleep(1500);
- }
- }
- void board()
- {
- system("CLS");
- cout << " | | " << endl; // gameboard start
- cout << " " << n[1] << " | " << n[2] << " | " << n[3] << " " << endl;
- cout << " ___|___|___" << endl;
- cout << " | | " << endl;
- cout << " " << n[4] << " | " << n[5] << " | " << n[6] << " " << endl;
- cout << " ___|___|___" << endl;
- cout << " | | " << endl;
- cout << " " << n[7] << " | " << n[8] << " | " << n[9] << " " << endl;
- cout << " | | " << endl; // gameboard end
- cout << endl;
- }
- int main()
- {
- board();
- //debug();
- while (win == 0)
- {
- choice();
- board();
- //debug();
- checkwin();
- }
- turn();
- system("CLS");
- cout << "Player " << player << " wins!" << endl;
- system("pause");
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement