Programming

Taka chwila dla debila ;]


#1 2008-10-05 10:28:33

Will

Administrator

Zarejestrowany: 2008-09-30
Posty: 12
Punktów :   

TicTacToe

Kod:

#include<iostream>
#include<string>
#include<sstream>
using namespace std;
string mapa="_______\n|1|2|3|\n|4|5|6|\n|7|8|9|\n-------";
bool tab[9]={false};
int playerArr[9];

void Ruch(
           bool player1)
{

int pole;
cout<<"\nPodaj pole: ";
cin>>pole;

if(tab[pole-1]==false)
{
char znak;
if(player1)
{
playerArr[pole-1]=1;
tab[pole-1]=true;
znak='x';
}
else
{
playerArr[pole-1]=0;
tab[pole-1]=true;
znak='o';
}
stringstream ss;
ss<<pole;
string miejsce;
ss>>miejsce;
int pos=mapa.find(miejsce);
mapa[pos]=znak;




}
else
{//zajete jeszce raz probuje
Ruch(player1);

}
}


bool Sprawdz();
void Wygrana();
bool ActualPlayer=0;


int main()
{
    for(int a=0;a<9;a++){playerArr[a]=-1;}
    cout<<mapa;
while(1)
{
    ActualPlayer=!ActualPlayer;
    Ruch(ActualPlayer);
    cout<<mapa;
    if(Sprawdz())break;
}


system("pause");
}


bool Sprawdz()
{
if(( playerArr[0]==0 &&playerArr[4]==0 &&playerArr[8]==0)  ||
    ( playerArr[0]==1 &&playerArr[4]==1 &&playerArr[8]==1) ||
    ( playerArr[2]==0 &&playerArr[4]==0 &&playerArr[6]==0) ||
    ( playerArr[2]==1 &&playerArr[4]==1 &&playerArr[6]==1))
{
    Wygrana();
    return true;
}
//dla rzedow
int pos=0;
for(int b=0;b<2;b++)
{
for(int a=0;a<3;a++)
{
if( playerArr[pos]==b &&playerArr[pos+1]==b &&playerArr[pos+2]==b)
{
    Wygrana();
    return true;
}
pos+=3;
}
pos=0;
}
//dla kolumn
pos=0;
for(int b=0;b<2;b++)
{
for(int a=0;a<3;a++)
{
if( playerArr[pos]==b &&playerArr[pos+3]==b &&playerArr[pos+6]==b)
{
    Wygrana();
    return true;
}
pos+=1;
}
pos=0;
}

return false;
}




//wygrana 
void Wygrana()
{
    cout<<"\nKoniec Gry! Wygral gracz:";
    if(ActualPlayer)cout<<"1";
    else cout<<"2";

}

Prosta wersja gry kółkoVSkrzyżyk. Aby zrozumieć program potrzeba znać podstawowe operacje+funkcje+tablice.
Jak ktoś czegoś nie rozumie pytać.
Jak coś o tablicach krótko można poczytać tu:
http://www.warp.nazwa.pl/Warp_strona/cc … .php?id=17
od strony 41.

Offline

 

Stopka forum

RSS
Powered by PunBB
© Copyright 2002–2008 PunBB
Polityka cookies - Wersja Lo-Fi

[ Generated in 0.059 seconds, 10 queries executed ]


Darmowe Forum | Ciekawe Fora | Darmowe Fora
www.hala69.pun.pl www.coldara.pun.pl www.350z.pun.pl www.polishsupermanager.pun.pl www.dod-wow.pun.pl