[ 1. #include <iostream.h>
2. #include <stdlib.h>
3.
4. char matrix[3][3];//={0};
5. void cou(void);
6. int main()
7. {
9. int m,n;
10. char ch='y';
11. while(ch=='Y'||ch=='y'){
12. for (m=0;m<3;m++)for (n=0;n<3;n++)matrix[m][n]= '\0';
13. int i,j,sum=0;
14. while ( sum < 10){
15. if (sum == 0) cou();
16. cout<<"Player 1 er 'X': Vælg rækkerne og kolonne"<<endl;
17. cout<<"Række : ";
18. cin>>i;
19. cout<<"kolonne : ";
20. cin>>j;
21. 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;}
22. matrix[i-1][j-1]='X';
23. sum++;
24. cou();
25.
26. //check if wins
27. if (matrix[0][0]=='X' && matrix[0][0]==matrix[1][1] && matrix[1][1]==matrix[2][2]) {cout<<"Player 1 wins"<<endl;break;}
28. if (matrix[2][0]=='X' && matrix[2][0]==matrix[1][1] && matrix[1][1]==matrix[0][2]) {cout<<"Player 1 wins"<<endl;break;}
29. if (matrix[0][0]=='X' && matrix[0][0]==matrix[1][0] && matrix[1][0]==matrix[2][0]) {cout<<"Player 1 wins"<<endl;break;}
30. if (matrix[0][1]=='X' && matrix[0][1]==matrix[1][1] && matrix[1][1]==matrix[2][1]) {cout<<"Player 1 wins"<<endl;break;}
31. if (matrix[0][2]=='X' && matrix[0][2]==matrix[1][2] && matrix[1][2]==matrix[2][2]) {cout<<"Player 1 wins"<<endl;break;}
32. if (matrix[0][0]=='X' && matrix[0][0]==matrix[0][1] && matrix[0][1]==matrix[0][2]) {cout<<"Player 1 wins"<<endl;break;}
33. if (matrix[1][0]=='X' && matrix[1][0]==matrix[1][1] && matrix[1][1]==matrix[1][2]) {cout<<"Player 1 wins"<<endl;break;}
34. if (matrix[2][0]=='X' && matrix[2][0]==matrix[2][1] && matrix[2][1]==matrix[2][2]) {cout<<"Player 1 wins"<<endl;break;}
35.
36. if (sum == 9){cout<<"Spillet er slut og ingen vinder. Hahaha, i begge nogle loser !!!!"<<endl; break;} //sum=9 because there are only 9 boxes in the game
37. //player 2's tur
38.
39. cout<<"Player 2 er 'O': vælg række og kolonne"<<endl;
40. cout<<"Række : ";
41. cin>>i;
42. cout<<"kolonne : ";
43. cin>>j;
44. 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;}
45. matrix[i-1][j-1]='O';
46. sum++;
47. //the play box
48. cou();
49. //check if wins
50. if (matrix[0][0]=='O' && matrix[0][0]==matrix[1][1] && matrix[1][1]==matrix[2][2]) {cout<<"Player 2 vinder"<<endl;break;}
51. if (matrix[2][0]=='O' && matrix[2][0]==matrix[1][1] && matrix[1][1]==matrix[0][2]) {cout<<"Player 2 vinder"<<endl;break;}
52. if (matrix[0][0]=='O' && matrix[0][0]==matrix[1][0] && matrix[1][0]==matrix[2][0]) {cout<<"Player 2 vinder"<<endl;break;}
53. if (matrix[0][1]=='O' && matrix[0][1]==matrix[1][1] && matrix[1][1]==matrix[2][1]) {cout<<"Player 2 vinder"<<endl;break;}
54. if (matrix[0][2]=='O' && matrix[0][2]==matrix[1][2] && matrix[1][2]==matrix[2][2]) {cout<<"Player 2 vinder"<<endl;break;}
55. if (matrix[0][0]=='O' && matrix[0][0]==matrix[0][1] && matrix[0][1]==matrix[0][2]) {cout<<"Player 2 vinder"<<endl;break;}
56. if (matrix[1][0]=='O' && matrix[1][0]==matrix[1][1] && matrix[1][1]==matrix[1][2]) {cout<<"Player 2 vinder"<<endl;break;}
57. if (matrix[2][0]=='O' && matrix[2][0]==matrix[2][1] && matrix[2][1]==matrix[2][2]) {cout<<"Player 2 vinder"<<endl;break;}
58.
59. }
60. cout<<"\nHar du lyst til at spille igen??? (Y - N)\n";
61. cin>>ch;
62. }
63. system("PAUSE");
64. return 0;
65. }
66.
67.
68.
69.
70. void cou(void)
71. {
72. //the play box
73. cout<<"\n\t\t 1 2 3\n"<<endl;
74. cout<<"\t\t 1 "<<matrix[0][0]<<" | "<<matrix[0][1]<<" | "<<matrix[0][2]<<endl;
75. cout<<"\t\t ---|---|---\n";
76. cout<<"\t\t 2 "<<matrix[1][0]<<" | "<<matrix[1][1]<<" | "<<matrix[1][2]<<endl;
77. cout<<"\t\t ---|---|---\n";
78. cout<<"\t\t 3 "<<matrix[2][0]<<" | "<<matrix[2][1]<<" | "<<matrix[2][2]<<"\n\n\n";
79. }