Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //tic tac toe game
- #include <iostream>
- using namespace std;
- int const SIZE = 4;
- int main() {
- char matrix[SIZE][SIZE]{ { ' ' ,'A','B','C' },{ '1',' ',' ',' '},{ '2',' ',' ',' '},{ '3',' ',' ',' '} };
- int i, j, k;
- char ch;
- int n;
- bool win = false;
- bool isX = true;
- for (k = 1; k <= 9 && !win; k++) {
- //displaying game status for x
- if (matrix[1][3] == 'x' && matrix[2][2] == 'x' && matrix[3][1] == 'x') {
- cout << " X wins!" << endl;
- win = true;
- break;
- }
- if (matrix[1][1] == 'x' && matrix[2][2] == 'x' && matrix[3][3] == 'x') {
- cout << " X wins!" << endl;
- win = true;
- break;
- }
- if (matrix[1][1] == 'x' && matrix[1][2] == 'x' && matrix[1][3] == 'x') {
- cout << " X wins!" << endl;
- win = true;
- break;
- }
- if (matrix[2][1] == 'x' && matrix[2][2] == 'x' && matrix[2][3] == 'x') {
- cout << " X wins!" << endl;
- win = true;
- break;
- }
- if (matrix[3][1] == 'x' && matrix[3][2] == 'x' && matrix[3][3] == 'x') {
- cout << " X wins!" << endl;
- win = true;
- }
- if (matrix[1][1] == 'x' && matrix[2][1] == 'x' && matrix[3][1] == 'x') {
- cout << " X wins!" << endl;
- win = true;
- break;
- }
- if (matrix[1][2] == 'x' && matrix[2][2] == 'x' && matrix[3][2] == 'x') {
- cout << " X wins!" << endl;
- win = true;
- break;
- }
- if (matrix[1][3] == 'x' && matrix[2][3] == 'x' && matrix[3][3] == 'x') {
- cout << " X wins!" << endl;
- win = true;
- break;
- }
- //displaying game status for O
- if (matrix[1][3] == 'o' && matrix[2][3] == 'o' && matrix[3][3] == 'o') {
- cout << " O wins!" << endl;
- win = true;
- break;
- }
- if (matrix[1][3] == 'o' && matrix[2][2] == 'o' && matrix[3][1] == 'o') {
- cout << " O wins!" << endl;
- win = true;
- break;
- }
- if (matrix[1][1] == 'o' && matrix[2][2] == 'o' && matrix[3][3] == 'o') {
- cout << " O wins!" << endl;
- win = true;
- break;
- }
- if (matrix[1][1] == 'o' && matrix[1][2] == 'o' && matrix[1][3] == 'o') {
- cout << " O wins!" << endl;
- win = true;
- break;
- }
- if (matrix[2][1] == 'o' && matrix[2][2] == 'o' && matrix[2][3] == 'o') {
- cout << " O wins!" << endl;
- win = true;
- break;
- }
- if (matrix[3][1] == 'o' && matrix[3][2] == 'o' && matrix[3][3] == 'o') {
- cout << " O wins!" << endl;
- win = true;
- break;
- }
- if (matrix[1][1] == 'o' && matrix[2][1] == 'o' && matrix[3][1] == 'o') {
- cout << " O wins!" << endl;
- win = true;
- break;
- }
- if (matrix[1][2] == 'o' && matrix[2][2] == 'o' && matrix[3][2] == 'o') {
- cout << " O wins!" << endl;
- win = true;
- break;
- }
- if (isX == true) {
- cout << "* Player number 1 (X) * " << endl;
- cout << "Please enter cell input: ";
- cin >> ch >> n;
- cout << endl;
- if (ch == 'A' && n == 1) {
- matrix[1][1] = 'x';
- }
- if (ch == 'A' && n == 2) {
- matrix[2][1] = 'x';
- }
- if (ch == 'A' && n == 3) {
- matrix[3][1] = 'x';
- }
- if (ch == 'B' && n == 1) {
- matrix[1][2] = 'x';
- }
- if (ch == 'B' && n == 2) {
- matrix[2][2] = 'x';
- }
- if (ch == 'B' && n == 3) {
- matrix[3][2] = 'x';
- }
- if (ch == 'C' && n == 1) {
- matrix[1][3] = 'x';
- }
- if (ch == 'C' && n == 2) {
- matrix[2][3] = 'x';
- }
- if (ch == 'C' && n == 3) {
- matrix[3][3] = 'x';
- }
- //printing the matrix
- for (i = 0; i < SIZE; i++) {
- for (j = 0; j < SIZE; j++) {
- cout << matrix[i][j] << "\t";
- }
- cout << endl << endl;
- }
- }
- if (isX == false) {
- cout << "* Player number 2 (O) * " << endl;
- cout << "Please enter cell input: ";
- cin >> ch >> n;
- cout << endl;
- if (ch == 'A' && n == 1) {
- matrix[1][1] = 'o';
- }
- if (ch == 'A' && n == 2) {
- matrix[2][1] = 'o';
- }
- if (ch == 'A' && n == 3) {
- matrix[3][1] = 'o';
- }
- if (ch == 'B' && n == 1) {
- matrix[1][2] = 'o';
- }
- if (ch == 'B' && n == 2) {
- matrix[2][2] = 'o';
- }
- if (ch == 'B' && n == 3) {
- matrix[3][2] = 'o';
- }
- if (ch == 'C' && n == 1) {
- matrix[1][3] = 'o';
- }
- if (ch == 'C' && n == 2) {
- matrix[2][3] = 'o';
- }
- if (ch == 'C' && n == 3) {
- matrix[3][3] = 'o';
- }
- //printing the matrix
- for (i = 0; i < SIZE; i++) {
- for (j = 0; j < SIZE; j++) {
- cout << matrix[i][j] << "\t";
- }
- cout << endl << endl;
- }
- }
- isX = !isX;
- }
- if (win == false) {
- cout << "it's a tie" << endl;
- }
- system("pause");
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement