Du kan jo lige tage et kig på min vens program
Det vil være dejligt hvis du kunne hjælpe mig..
-
#include <iostream.h>
#include <stdlib.h>
#include <conio.h>
char matrix[3][3];//={0};
void cou(void);
int main()
{
int m,n;
char ch='y';
while(ch=='Y'||ch=='y'){
for (m=0;m<3;m++)for (n=0;n<3;n++)matrix[m][n]= '\0';
int i,j,sum=0;
while ( sum < 10){
if (sum == 0) cou();
cout<<"Player 1 'X': Vaelg Raekkerne og kolonne"<<endl;
cout<<"Raekke : ";
cin>>i;
cout<<"kolonne : ";
cin>>j;
for (;i>3 || i<1 || j>3 || j<1 ||('X'==matrix[i-1][j-1]||'O'==matrix[i-1][j-1])
{cout<<"Undskyld min ven, men du boer vaelge en anden plads.\n";cout<<"row : ";cin>>i;cout<<"column : ";cin>>j;}
matrix[i-1][j-1]='X';
sum++;
cou();
//check if wins
if (matrix[0][0]=='X' && matrix[0][0]==matrix[1][1] && matrix[1][1]==matrix[2][2]) {cout<<"Player 1 vinder"<<endl;break;}
if (matrix[2][0]=='X' && matrix[2][0]==matrix[1][1] && matrix[1][1]==matrix[0][2]) {cout<<"Player 1 vinder"<<endl;break;}
if (matrix[0][0]=='X' && matrix[0][0]==matrix[1][0] && matrix[1][0]==matrix[2][0]) {cout<<"Player 1 vinder"<<endl;break;}
if (matrix[0][1]=='X' && matrix[0][1]==matrix[1][1] && matrix[1][1]==matrix[2][1]) {cout<<"Player 1 vinder"<<endl;break;}
if (matrix[0][2]=='X' && matrix[0][2]==matrix[1][2] && matrix[1][2]==matrix[2][2]) {cout<<"Player 1 vinder"<<endl;break;}
if (matrix[0][0]=='X' && matrix[0][0]==matrix[0][1] && matrix[0][1]==matrix[0][2]) {cout<<"Player 1 vinder"<<endl;break;}
if (matrix[1][0]=='X' && matrix[1][0]==matrix[1][1] && matrix[1][1]==matrix[1][2]) {cout<<"Player 1 vinder"<<endl;break;}
if (matrix[2][0]=='X' && matrix[2][0]==matrix[2][1] && matrix[2][1]==matrix[2][2]) {cout<<"Player 1 wins"<<endl;break;}
if (sum == 9){cout<<"Spillet er slut og ingen vinder. Hahaha, i begge nogle loser !!!!"<<endl; break;} //sum=9 fordi der kun er 9 bokse i spillet
//player 2's tur
cout<<"Player 2 er 'O': vælg raekke og kolonne"<<endl;
cout<<"Raekke : ";
cin>>i;
cout<<"kolonne : ";
cin>>j;
for (;i>3 || i<1 || j>3 || j<1 ||('X'==matrix[i-1][j-1]||'O'==matrix[i-1][j-1])
{cout<<" Undskyld min ven, men du boer vaelge en anden plads.\n";cout<<"row : ";cin>>i;cout<<"kolonne : ";cin>>j;}
matrix[i-1][j-1]='O';
sum++;
//the play box
cou();
//check if wins
if (matrix[0][0]=='O' && matrix[0][0]==matrix[1][1] && matrix[1][1]==matrix[2][2]) {cout<<"Player 2 vinder"<<endl;break;}
if (matrix[2][0]=='O' && matrix[2][0]==matrix[1][1] && matrix[1][1]==matrix[0][2]) {cout<<"Player 2 vinder"<<endl;break;}
if (matrix[0][0]=='O' && matrix[0][0]==matrix[1][0] && matrix[1][0]==matrix[2][0]) {cout<<"Player 2 vinder"<<endl;break;}
if (matrix[0][1]=='O' && matrix[0][1]==matrix[1][1] && matrix[1][1]==matrix[2][1]) {cout<<"Player 2 vinder"<<endl;break;}
if (matrix[0][2]=='O' && matrix[0][2]==matrix[1][2] && matrix[1][2]==matrix[2][2]) {cout<<"Player 2 vinder"<<endl;break;}
if (matrix[0][0]=='O' && matrix[0][0]==matrix[0][1] && matrix[0][1]==matrix[0][2]) {cout<<"Player 2 vinder"<<endl;break;}
if (matrix[1][0]=='O' && matrix[1][0]==matrix[1][1] && matrix[1][1]==matrix[1][2]) {cout<<"Player 2 vinder"<<endl;break;}
if (matrix[2][0]=='O' && matrix[2][0]==matrix[2][1] && matrix[2][1]==matrix[2][2]) {cout<<"Player 2 vinder"<<endl;break;}
}
cout<<"\nHar du lyst til at spille igen??? (Y - N)\n";
cin>>ch;
}
system("PAUSE");
return 0;
}
void cou(void)
{
//the play box
cout<<"\n\t\t 1 2 3\n"<<endl;
cout<<"\t\t 1 "<<matrix[0][0]<<" | "<<matrix[0][1]<<" | "<<matrix[0][2]<<endl;
cout<<"\t\t ---|---|---\n";
cout<<"\t\t 2 "<<matrix[1][0]<<" | "<<matrix[1][1]<<" | "<<matrix[1][2]<<endl;
cout<<"\t\t ---|---|---\n";
cout<<"\t\t 3 "<<matrix[2][0]<<" | "<<matrix[2][1]<<" | "<<matrix[2][2]<<"\n\n\n";
}
Indlæg senest redigeret d. 04.04.2012 12:25 af Bruger #17134