tic-tac-toe does not work
Dec 2, 2012 at 7:52pm UTC
Hi : > I wrote a tic-tac-toe program. It works except top left corner. I can't put anything until decisive move. I will be grateful for your support, I'm total n00b.
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
#include <iostream>
#include<conio.h>
using namespace std;
void tab(char plansza[])
{
for (int i = 1; i <= 9; i++)
{
cout << " " << plansza[i] << " " ;
if (i % 3) cout << "|" ;
else if (i != 9) cout << "\n---@---@---\n" ;
else cout << endl;
}
}
bool zwyciestwo(char plansza[], char a)
{
bool test;
test = false ;
for (int i=1; i<4;i++)
test|=( (plansza[i] == a) && (plansza[i+3] == a) && (plansza[i+6] == a));
for (int i=1; i<8;i=i+3)
test|=( (plansza[i] == a) && (plansza[i+1] ==a) && (plansza[i+2] == a));
test |= ((plansza[1] == a) && (plansza[5] ==a) && (plansza[9] == a));
test |= ((plansza[3] == a) && (plansza[5] == a) && (plansza[7] == a));
if (test == true )
{
tab(plansza);
cout<<"wygrywa gracz " << a <<". Gratulacje!" ;
return true ;
}
else return false ;
}
bool remis(char plansza[])
{
for (int i=1; i<10; i++)
if (plansza[i] = ' ' ) {return false ;}
else
{ tab(plansza);
cout<<"REMIS" ;
return true ; }
}
void kolejka(char plansza[], char &gracz)
{
int n;
tab(plansza);
cout<< gracz <<" teraz twoj ruch: " ;
cin>> n;
if ((n >= 1) && (n <= 9) && (plansza[n] ==' ' )) plansza[n] = gracz;
if (gracz == 'O' ) gracz = 'X' ;
else gracz = 'O' ;
}
main()
{
char pole[10],a;
cout<<"----+---+---+---+---+---+---+---+---+---+---+" <<endl;
cout<<"GRA W KOLKO I KRZYZYK " <<endl<<"dla graczy x i o. Jako pierwszy porusza sie gracz x. Aby wykonac ruch podaj numer pola" <<endl;
cout<<"----+---+---+---+---+---+---+---+---+---+---+" <<endl<<endl;
for (int i=1; i <= 9; i++) pole[i] =' ' ;
a = 'X' ;
while (!zwyciestwo(pole,'X' ) && !zwyciestwo(pole,'O' ) && !remis(pole)) kolejka(pole,a);
getch();
}
Dec 2, 2012 at 7:54pm UTC
Arrays start at 0. You're starting at 1, therefore you're not able to use the corner.
Just change your 1's to zeros and have your if statements be only <9, not <=9.
Dec 2, 2012 at 8:01pm UTC
Thank you, I am extremely grateful. :-)
Topic archived. No new replies allowed.