Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- cout << "CPU takes its turn...n";
- if (box[2] == box[3] || box[4] == box[7] ||
- box[5] == box[9] && box[1] != 'X' || box[1] != 'O') return 1;
- if (box[1] == box[3] || box[5] == box[8] && box[2] != 'X' || box[2] != 'O') return 2;
- if (box[1] == box[2] || box[6] == box[9] && box[3] != 'X' || box[3] != 'O') return 3;
- if (box[1] == box[7] || box[5] == box[6] && box[4] != 'X' || box[4] != 'O') return 4;
- if (box[1] == box[9] || box[4] == box[6] ||
- box[2] == box[8] || box[3] == box[7] && box[5] != 'X' || box[5] != 'O') return 5;
- if (box[3] == box[9] || box[4] == box[5] && box[6] != 'X' || box[6] != 'O') return 6;
- if (box[1] == box[4] || box[8] == box[9] ||
- box[3] == box[5] && box[7] != 'X' || box[7] != 'O') return 7;
- if (box[7] == box[9] || box[2] == box[5] && box[8] != 'X' || box[8] != 'O') return 8;
- if (box[1] == box[5] || box[7] == box[8] ||
- box[3] == box[6] && box[9] != 'X' || box[9] != 'O') return 9;
- else {
- }
- }
- #include <iostream>
- #include <cstdlib>
- using namespace std;
- char box[10] = {'v','1','2','3','4','5','6','7','8','9'};
- int checkwin();
- void board();
- int CPUmove();
- int main()
- {
- bool exit = true;
- while (exit){
- box[0] = 'v';
- box[1] = '1';
- box[2] = '2';
- box[3] = '3';
- box[4] = '4';
- box[5] = '5';
- box[6] = '6';
- box[7] = '7';
- box[8] = '8';
- box[9] = '9';
- int player = 1, i, choice,intexit;
- char mark;
- srand(time(0));
- do
- {
- board();
- player = (player % 2) ? 1 : 2;
- if (player == 1){
- cout << "Enter the number of the box you want to choose: ";
- cin >> choice;
- }
- else if (player == 2) choice = CPUmove();
- mark = (player == 1) ? 'X' : 'O';
- if (box[choice] == (char)(((int)'0')+choice))
- box[choice] = mark;
- else
- {
- cout << "Invalid move ";
- player--;
- cin.ignore();
- cin.get();
- }
- i = checkwin();
- player++;
- } while(i == -1);
- board();
- if (i == 1) cout << "==>aPlayer " << --player << " wins!";
- else cout << "==>aTie game, you both stink.";
- cout<<"do you want to play an other game: press 1 else: press 2: ";
- cin>>intexit;
- if (intexit==2)
- exit = false;
- cin.ignore();
- cin.get();
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement