TIC-TAC-TOE

Allright guys. All of us have or will be going through this console program sooner or later. And now it's my turn.

A book told me I could create a game board for TIC-TAC -TOE using multidimensional arrays, so I did.

This is what I HAVE done so far:

- Initialization (Start menu)

- Game Loop (Looping through X and O's input 9 times)

Display board (Displaying an empty board)


This is what I HAVEN'T done so far, and need help with:

- Get the players move and translate it to the board
(example: input X = 1, X get's updated to the board location (1) out of (9).


So this is what I need help with. What is the easiest way to check if a user O or user X already have entered a specific value? And how do I update it to the board?

I don't hope I have to put in a BUNCH of if - else statements that states EVERY single possible situation, do I?

This is my source file so far:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
#include <iostream>
#include <cmath>
#include <string>
#include <cstdlib>
using namespace std;

int choice;
string X = "X";
string O = "O";
int inputX;
int inputO;

/* WELCOME MESSAGE */
void welcome(){
cout << "WELCOME TO TIC-TAC-TOE" << endl << endl;
}

/* GAME INSTRUCTIONS */
void instructions(){
cout << endl;
cout << "TIC-TAC TOE is all about getting 3 lines in a row, horizontally or vertacally, or both." << endl << endl;
cout << "It is up to the player whether he/her chooses the 'O' or the 'X' to play with." << endl << endl << endl;
cout << "               NOTE: TYPE '-1' TO EXIT THE GAME ANYTIME!" << endl << endl;
cout << "                 This game was made using the console." << endl << endl;
}

/*GAME BOARD*/
void board(){
string multi_array[3][3] = {{"|_|", "|_|", "|_|"}, {"|_|", "|_|", "|_|"}, {"|_|", "|_|", "|_|"}};
for(int row = 0; row < 3; row++){
        for(int column = 0; column < 3; column++){
            cout << multi_array[row][column] << "";
        }
        cout << endl;
    }
}


/* GAME INPUT */
void input(){
do{
cout << endl;
board();
cout << "Please enter a number betweeen 0 and 8" << endl;
    for(int times = 0; times < 9; times++){
    do{
        if(times == 0){
            cout << endl << X << " starts: ";
                cin >> inputX;
        }
        else if(times == 2 || times == 4 || times == 6 || times == 8){
            cout << endl << X << "'s turn: ";
                cin >> inputX;
        }
        else{
            cout << endl << O << "'s turn: ";
                cin >> inputO;
        }
        if(inputX == -1 || inputO == -1){
            cout << "Thankyou for playing!" << endl << endl;
            exit(EXIT_FAILURE);
        }
        if(inputX > 8 || inputO > 8 || inputX < 0 || inputO < 0){
            cout << "Invalid input! You can only declare < 0 - 8 >" << endl << endl;
        }

    }while(inputX > 8 || inputO > 8 || inputX < 0 || inputO < 0);
        }  cout << endl << endl << "NEW ROUND" << endl;
            }while( ! (inputX == -1 || inputO == -1) );
}

/* GAME MENU */
void menu(){
cout << "1. Play" << endl;
cout << "2. Instructions" << endl << endl;
}

/* MENU AFTER READING INSTRUCTIONS */
void menu1(){
cout << "1. Play" << endl;
cin >> choice;
    if(choice == 1){
        input();
    }
    else if(choice == 2){
        cout << endl << "You have already read the instructions!" << endl;
    }
    else{
        cout << endl << "You didn't enter a valid option!" << endl;
    }
}

/* MENU CHOICE */
void menu_choice(){
cout << "< 1 || 2 >?: ";
cin >> choice;
switch(choice){

case 1:
    input();
break;
case 2:
    instructions();
break;

default:
    cout << "You didn't enter a valid option!";
    }
    cout << endl << endl;
}

int main(){
welcome();
menu();
menu_choice();
    while( ! (choice == 1) ){
        menu1();
        }
return 0;
}



EDIT: Sorry if my code is a little messy! This is my first time writing a small game tho, so I hope it's fine ;P
Last edited on
Topic archived. No new replies allowed.