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 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243
|
#include <iostream>
#include <Windows.h>
using std::cout;
using std::endl;
using std::cin;
using std::string;
using namespace System;
enum Colors{
black, // 0 text color - multiply by 16, for background colors
dark_blue, // 1
dark_green, // 2
dark_cyan, // 3
dark_red, // 4
dark_magenta, // 5
dark_yellow, // 6
light_gray, // 7
dark_gray, // 8
light_blue, // 9
light_green, // 10
light_cyan, // 11
light_red, // 12
light_magenta, // 13
light_yellow, // 14
white // 15
};
void text(int text_color = 7 , int paper_color = 0)
{
// defaults to light_gray on black
int color_total = (text_color + (paper_color * 16));
SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), color_total);
}
void TTT_Piece(char piece)
{
if(piece == 'X')
{
text(light_blue, black);
}
else if(piece == 'O')
{
text(light_green, black);
}
else
text(light_yellow, black);
}
char tabla[3][3] = { '1', '2', '3', '4', '5', '6', '7', '8', '9' };
char potez = 'X';
int red, kolona;
bool izjednaceno = false;
//Tabla
void prikaz_table()
{
system("CLS");
char piece;
text(white, black);
cout << "\t\t\t\t ************************** IKS--OKS IGRA ********************************\n\n" << endl;
cout << "\t\t\t\t\t\t\t _____ _____ _____\n";
cout << "\t\t\t\t\t\t\t| | | |\n";
cout << "\t\t\t\t\t\t\t| ";
piece = tabla[0][0];
TTT_Piece(piece);
cout << tabla[0][0];
text(white, black);
cout << " | ";
piece = tabla[0][1];
TTT_Piece(piece);
cout << tabla[0][1];
text(white, black);
cout << " | ";
piece = tabla[0][2];
TTT_Piece(piece);
cout << tabla[0][2];
text(white, black);
cout << " |\n";
cout << "\t\t\t\t\t\t\t|_____|_____|_____| \n";
cout << "\t\t\t\t\t\t\t| | | |\n";
cout << "\t\t\t\t\t\t\t| ";
piece = tabla[1][0];
TTT_Piece(piece);
cout << tabla[1][0];
text(white, black);
cout << " | ";
piece = tabla[1][1];
TTT_Piece(piece);
cout << tabla[1][1];
text(white, black);
cout << " | ";
piece = tabla[1][2];
TTT_Piece(piece);
cout << tabla[1][2];
text(white, black);
cout << " |\n";
cout << "\t\t\t\t\t\t\t|_____|_____|_____|\n";
cout << "\t\t\t\t\t\t\t| | | |\n";
cout << "\t\t\t\t\t\t\t| ";
piece = tabla[2][0];
TTT_Piece(piece);
cout << tabla[2][0];
text(white, black);
cout << " | ";
piece = tabla[2][1];
TTT_Piece(piece);
cout << tabla[2][1];
text(white, black);
cout << " | ";
piece = tabla[2][2];
TTT_Piece(piece);
cout << tabla[2][2];
text(white, black);
cout << " |\n";
cout << "\t\t\t\t\t\t\t|_____|_____|_____| \n";
}
// IGRA U TOKU
void igra_u_toku() {
int polje;
if (potez == 'X') {
cout << "Igrac 1 na potezu [X]: ";
cin >> polje;
}
else if (potez == 'O') {
cout << "Igrac 2 na potezu [O]: ";
cin >> polje;
}
//cin >> polje;
switch (polje) {
case 1: red = 0; kolona = 0; break;
case 2: red = 0; kolona = 1; break;
case 3: red = 0; kolona = 2; break;
case 4: red = 1; kolona = 0; break;
case 5: red = 1; kolona = 1; break;
case 6: red = 1; kolona = 2; break;
case 7: red = 2; kolona = 0; break;
case 8: red = 2; kolona = 1; break;
case 9: red = 2; kolona = 2; break;
}
if (potez == 'X' && tabla[red][kolona] != 'X' && tabla[red][kolona] != 'O') {
tabla[red][kolona] = 'X';
potez = 'O';
}
else if (potez == 'O' && tabla[red][kolona] != 'X' && tabla[red][kolona] != 'O') {
tabla[red][kolona] = 'O';
potez = 'X';
}
else {
cout << "To polje je zauzeto.Pokusajte sa drugim poljem.\n";
igra_u_toku();
}
prikaz_table();
}
//KRAJ IGRE
bool kraj_igre() {
for (int i = 0; i < 3; i++) {
if ((tabla[i][0] == tabla[i][1] && tabla[i][0] == tabla[i][2]) ||
(tabla[0][i] == tabla[1][i] && tabla[0][i] == tabla[2][i]) ||
(tabla[0][0] == tabla [1][1] && tabla[1][1] == tabla[2][2]) ||
(tabla[0][2] == tabla[1][1] && tabla[1][1] == tabla[2][0]))
{
return false;
}
}
for (int i = 0; i < 3; i++) {
for (int j = 0; j < 3; j++) {
if (tabla[i][j] != 'X' && tabla[i][j] != 'O') {
return true;
}
}
}
izjednaceno = true;
return false;
}
int main()
{
char odluka='d';
bool ok;
do
{
do {
prikaz_table();
igra_u_toku();
ok = kraj_igre();
if (potez == 'X' && izjednaceno == false)
{
//system("CLS");
cout << "DRUGI IGRAC JE POBEDIO [O] !!!!!!!!!!!!!!\n";
}
else if (potez == 'O' && izjednaceno == false) {
cout << "PRVI IGRAC JE POBEDIO [X] !!!!!!!!!!!!!!!\n";
}
}while(ok);
do
{
cout << "IGRA JE IZJEDNACENA!!!!!!!!!!!!\n";
cout << "Da li zelite da igrate opet [DA] - d ili [NE] - n : ";
cin >> odluka;
if ( odluka != 'd' && odluka != 'n')
{
cout << "Uneli ste nepostojeci simbol. Pokusajte ponovo.";
}
}while( odluka != 'd' && odluka != 'n');
int x=1;
for (int i = 0; i < 3; i++) // New game. Reset the array to original values
{
for (int j = 0; j < 3; j++)
{
tabla[i][j] = '0'+x;
x++;
}
}
}while(odluka == 'd');
system("PAUSE");
return 0;
}
|