beginner Tic Tac Toe Game
Oct 11, 2013 at 6:44pm UTC
so basically what im trying to do is set the private variable that is printed in the grid function to a value that is specified by the user. I can change any variable i want statically (changing string x in the set_box function) to a user input (string y), but i'd like to let the user input two strings, the first to identify a variable, and a second to specify what you want the identified variable to be set equal to.
it seems like there should be a really easy way to do this, but i just cant think of it. im pretty new to programming and this forum, so i apologize if this question is either ignorant, or misplaced.
thanks
-nick
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
#include <iostream>
#include <string>
using namespace std;
class board{
public :
void set_box(string, string);
void grid();
private :
string a1 = " " ;
string a2 = " " ;
string a3 = " " ;
string b1 = " " ;
string b2 = " " ;
string b3 = " " ;
string c1 = " " ;
string c2 = " " ;
string c3 = " " ;
};
int main()
{
string a;
string b;
board grid;
cout << "::::: " ;
cin >> a >> b;
grid.set_box(a, b);
grid.grid();
}
void board:: grid (){
cout << "___________________________" << endl;
cout << "| | | |" << endl;
cout << "| " ;
cout << a1;
cout << " | " ;
cout << a2;
cout << " | " ;
cout << a3;
cout << " |" << endl;
cout << "| | | |" << endl;
cout << "___________________________" << endl;
cout << "| | | |" << endl;
cout << "| " ;
cout <<b1;
cout << " | " ;
cout << b2;
cout << " | " ;
cout << b3;
cout << " |" << endl;
cout << "| | | |" << endl;
cout << "___________________________" << endl;
cout << "| | | |" << endl;
cout << "| " ;
cout << c1;
cout << " | " ;
cout << c2;
cout << " | " ;
cout << c3;
cout << " |" << endl;
cout << "| | | |" << endl;
cout << "___________________________" << endl;
}
void board::set_box(string x, string y){
x = y;
}
Oct 11, 2013 at 7:54pm UTC
I hope that works:
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 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160
#include <iostream>
#include <string>
using namespace std;
class board{
public :
board();
void set_box( int );
void grid();
private :
string a1;
string a2;
string a3;
string b1;
string b2;
string b3;
string c1;
string c2;
string c3;
int cur_play;
};
int main()
{
string a;
string b;
board grid;
grid.grid();
cout << "::::: " ;
cin >> a;
grid.set_box( a );
}
void board:: grid (){
cout << "___________________________" << endl;
cout << "| | | |" << endl;
cout << "| " ;
cout << a1;
cout << " | " ;
cout << a2;
cout << " | " ;
cout << a3;
cout << " |" << endl;
cout << "| | | |" << endl;
cout << "___________________________" << endl;
cout << "| | | |" << endl;
cout << "| " ;
cout <<b1;
cout << " | " ;
cout << b2;
cout << " | " ;
cout << b3;
cout << " |" << endl;
cout << "| | | |" << endl;
cout << "___________________________" << endl;
cout << "| | | |" << endl;
cout << "| " ;
cout << c1;
cout << " | " ;
cout << c2;
cout << " | " ;
cout << c3;
cout << " |" << endl;
cout << "| | | |" << endl;
cout << "___________________________" << endl;
}
void board::set_box( int x )
{
string sign;
if ( cur_play == 1 )
{
sign = "X" ;
cur_play = 2;
}
if ( cur_play == 2 )
{
sign = "O" ;
cur_play = 1;
}
switch ( x )
{
case 1:
a1 = sign;
break ;
case 2:
a2 = sign;
break ;
case 3:
a3 = sign;
break ;
case 4:
b1 = sign;
break ;
case 5:
b2 = sign;
break ;
case 6:
b3 = sign;
break ;
case 7:
c1 = sign;
break ;
case 8:
c2 = sign;
break ;
case 9:
c3 = sign;
break ;
}
}
board::board()
{
a1 = "1" ;
a2 = "2" ;
a3 = "3" ;
b1 = "4" ;
b2 = "5" ;
b3 = "6" ;
c1 = "7" ;
c2 = "8" ;
c3 = "9" ;
cur_play = 1;
}
EDIT: I am so sleepy that I dont know what I have coded so excuse me if there is any problem with that code.
Last edited on Oct 11, 2013 at 8:03pm UTC
Topic archived. No new replies allowed.