آقا من اولین بازیم رو به عنوان آخرین تمرینی که باید تو کلاس برنامه نویسی تحویل بدیم نوشتم!
بازی سنگین و پیچیده XO :cheesygri یا همون Tic Tac Toe
یکی دو روز مخ سوزوندم تا تکمیل شد تازه اونم Level ساده! ( کامپیوتر کاملاً رندوم خونه ش رو انتخاب میکنه و بسادگی میتونید برنده شید )
اگه intermediate , advance ش هم بنویسیم 40% بیشتر نمره میگیریم! این 2 هفته ای که تا امتحان مونده سعی میکنم اینم بنویسم ... باید بیشتر رو هوش مصنوعیش کار کنم
تست کنید اگه اشکالی چیزی داره بگید ...
system requirement :
any system !
فقط حواستون باشه برای اجرا باید آخرین نسخه Daemon tools , sfnightmare رو نصب داشته باشید ... کابل کشی هم لازم داره به این صورت که بعد از اتمامه بازی کابل برق رو میکشید
Source :
از اونجا که من مبتدی هستم! یه چیزی حدود 200 تا If , cout داره فکر کنم خیلی کوتاه تر از اینم میشد نوشت.
فقط امیدوارم بچه های کلاس اینجا رو پیدا نکنن و سورسه من لو نره
نمایی از بازی!! :
]
خود بازی و سورسش :
بازی سنگین و پیچیده XO :cheesygri یا همون Tic Tac Toe
یکی دو روز مخ سوزوندم تا تکمیل شد تازه اونم Level ساده! ( کامپیوتر کاملاً رندوم خونه ش رو انتخاب میکنه و بسادگی میتونید برنده شید )
اگه intermediate , advance ش هم بنویسیم 40% بیشتر نمره میگیریم! این 2 هفته ای که تا امتحان مونده سعی میکنم اینم بنویسم ... باید بیشتر رو هوش مصنوعیش کار کنم
تست کنید اگه اشکالی چیزی داره بگید ...
system requirement :
any system !
فقط حواستون باشه برای اجرا باید آخرین نسخه Daemon tools , sfnightmare رو نصب داشته باشید ... کابل کشی هم لازم داره به این صورت که بعد از اتمامه بازی کابل برق رو میکشید
Source :
// X O Game
#include <iostream.h>
#include <conio.h>
#include <stdlib.h>
#include <iomanip.h>
#include <time.h>
#include <string>
int main(void)
{
int row,colomn,n,r,s,h,p,q,t,m;
char a[3][3],choice,answer;
p=0; q=0; t=0; // p: your wins, q: your loses, t: ties
cout << "\t\t 1 2 3\n\n";
cout << "\t\t\t| " << "|" << endl;
cout << "\t\t 1 | " << "|" << endl;
cout << "\t\t ---|---|---" << endl;
cout << "\t\t 2 | " << "|" << endl;
cout << "\t\t ---|---|---" << endl;
cout << "\t\t 3 | " << "|" << endl;
cout << "\t\t\t| " << "|\n\n" << endl;
cout << "Welcome to the game of TIC-TAC-TOE!\n\n";
cout << "Please enter your first name:\n\n";
string name;
cin >> name;
do
{
cout<<"\nWell "<<name<<", let's see who has the first move, me or you!\n";
cout<<"Choose! Head or Tail ?(H/T)\n";
cin >> choice;
srand(time(NULL));
h = rand()%2; // It produce 0 or 1. if it's 0 user has the
if (h == 0) // first move, else computer has it.
cout << "\nYou are first! " << endl;
else
cout << "\nI'm first, You're next!" << endl;
bool keep = true;
for (int i=0; i<3; i++)
for (int j=0; j<3; j++)
a[j] = '\0'; // all the matrix cells are blank now.
n=0;
do
{
if (h == 0)
{
cout << "\nIt's your turn " << name <<"!" << endl;
do
{
cout<<"\nPlease enter the number of the row you wish to place in:"<<endl;
cin >> row;
if (row > 3 || row < 1)
cout<<"\nCaution! you must enter only 1 or 2 or 3 for the row\n";
}while(row > 3 || row < 1);
do
{
cout<<"Please enter the number of the colomn you wish to place in:"<<endl;
cin >> colomn;
if (colomn > 3 || colomn < 1)
cout<<"\nCaution! you must enter only 1 or 2 or 3 for the colomn\n";
}while (colomn > 3 || colomn < 1);
row = row - 1; // because in c++ arrays start from 0 not 1.
colomn = colomn - 1;
if (n>0 && a[row][colomn] != '\0')
{
do // checks that the cell is blank or not.
{
cout << "\nThis place is full ! select another\n" << endl;
cout<<"Please enter the number of the row you wish to place in:" << endl;
cin >> row;
cout<<"Please enter the number of the colomn you wish to place in:"<<endl;
cin >> colomn;
row = row - 1;
colomn = colomn - 1;
}while(a[row][colomn] != '\0');
}
a[row][colomn] = 88; // 88 is the number that produce X as a char.
cout<<"\t\t 1 2 3\n\n";
cout<<"\t\t\t| " << "|" << endl;
cout<<"\t 1 "<<a[0][0]<<" | "<<a[0][1]<<" | "<<a[0][2]<<endl;
cout<<"\t\t ---|---|---" << endl;
cout<<"\t 2 "<<a[1][0]<<" | "<<a[1][1]<<" | "<<a[1][2]<<endl;
cout<<"\t\t ---|---|---" << endl;
cout<<"\t 3 "<<a[2][0]<<" | "<<a[2][1]<<" | "<<a[2][2]<<endl;
cout<<"\t\t\t| " << "|\n" << endl;
if (a[0][0]==88 && a[0][1]==88 && a[0][2]==88) keep = false;
if (a[1][0]==88 && a[1][1]==88 && a[1][2]==88) keep = false;
if (a[2][0]==88 && a[2][1]==88 && a[2][2]==88) keep = false;
if (a[0][0]==88 && a[1][0]==88 && a[2][0]==88) keep = false;
if (a[0][1]==88 && a[1][1]==88 && a[2][1]==88) keep = false;
if (a[0][2]==88 && a[1][2]==88 && a[2][2]==88) keep = false;
if (a[0][0]==88 && a[1][1]==88 && a[2][2]==88) keep = false;
if (a[0][2]==88 && a[1][1]==88 && a[2][0]==88) keep = false;
}
m=0;
for (int i=0; i<3; i++)
for (int j=0; j<3; j++)
if (a[j] != '\0')
m++;
if (m == 9) // if m become 9 it means that all 9 cells are full
keep = false; // thus the game is tie and program should go out
// of the loop by making keep false.
if (keep == true)
{
do
{
r = rand()%3; // will produce 0 or 1 or 2.
s = rand()%3;
}while(a[r] != '\0'); // checks that the cell is blank or not.
a[r] = 79; // 79 is the number that produce O as a char.
cout << "Press any key for the computer's move \n\n";
getch();
cout<<"The computer placed an O in location "<<(r+1)<<","<<(s+1)<< endl;
cout<<"\n\t\t 1 2 3\n\n";
cout<<"\t\t\t| " << "|" << endl;
cout<<"\t 1 "<<a[0][0]<<" | "<<a[0][1]<<" | "<<a[0][2]<<endl;
cout<<"\t\t ---|---|---" << endl;
cout<<"\t 2 "<<a[1][0]<<" | "<<a[1][1]<<" | "<<a[1][2]<<endl;
cout<<"\t\t ---|---|---" << endl;
cout<<"\t 3 "<<a[2][0]<<" | "<<a[2][1]<<" | "<<a[2][2]<<endl;
cout<<"\t\t\t| " << "|\n" << endl;
}
if (a[0][0]==79 && a[0][1]==79 && a[0][2]==79) keep = false;
if (a[1][0]==79 && a[1][1]==79 && a[1][2]==79) keep = false;
if (a[2][0]==79 && a[2][1]==79 && a[2][2]==79) keep = false;
if (a[0][0]==79 && a[1][0]==79 && a[2][0]==79) keep = false;
if (a[0][1]==79 && a[1][1]==79 && a[2][1]==79) keep = false;
if (a[0][2]==79 && a[1][2]==79 && a[2][2]==79) keep = false;
if (a[0][0]==79 && a[1][1]==79 && a[2][2]==79) keep = false;
if (a[0][2]==79 && a[1][1]==79 && a[2][0]==79) keep = false;
h = 0;
n++;
}while(n < 5 && keep);
cout << "finish! last step is :" << endl;
cout << "\t\t\t| " << "|" << endl;
cout << "\t\t "<<a[0][0]<<" | "<<a[0][1]<<" | "<<a[0][2]<<endl;
cout << "\t\t ---|---|---" << endl;
cout << "\t\t "<<a[1][0]<<" | "<<a[1][1]<<" | "<<a[1][2]<<endl;
cout << "\t\t ---|---|---" << endl;
cout << "\t\t "<<a[2][0]<<" | "<<a[2][1]<<" | "<<a[2][2]<<endl;
cout << "\t\t\t| " << "|" << endl;
if (a[0][0]==88 && a[0][1]==88 && a[0][2]==88)
{cout << "you win !" << endl; p++;}
else if (a[1][0]==88 && a[1][1]==88 && a[1][2]==88)
{cout << "you win !" << endl; p++;}
else if (a[2][0]==88 && a[2][1]==88 && a[2][2]==88)
{cout << "you win !" << endl; p++;}
else if (a[0][0]==88 && a[1][0]==88 && a[2][0]==88)
{cout << "you win !" << endl; p++;}
else if (a[0][1]==88 && a[1][1]==88 && a[2][1]==88)
{cout << "you win !" << endl; p++;}
else if (a[0][2]==88 && a[1][2]==88 && a[2][2]==88)
{cout << "you win !" << endl; p++;}
else if (a[0][0]==88 && a[1][1]==88 && a[2][2]==88)
{cout << "you win !" << endl; p++;}
else if (a[0][2]==88 && a[1][1]==88 && a[2][0]==88)
{cout << "you win !" << endl; p++;}
else if (a[0][0]==79 && a[0][1]==79 && a[0][2]==79)
{cout << "you lose !" << endl; q++;}
else if (a[1][0]==79 && a[1][1]==79 && a[1][2]==79)
{cout << "you lose !" << endl; q++;}
else if (a[2][0]==79 && a[2][1]==79 && a[2][2]==79)
{cout << "you lose !" << endl; q++;}
else if (a[0][0]==79 && a[1][0]==79 && a[2][0]==79)
{cout << "you lose !" << endl; q++;}
else if (a[0][1]==79 && a[1][1]==79 && a[2][1]==79)
{cout << "you lose !" << endl; q++;}
else if (a[0][2]==79 && a[1][2]==79 && a[2][2]==79)
{cout << "you lose !" << endl; q++;}
else if (a[0][0]==79 && a[1][1]==79 && a[2][2]==79)
{cout << "you lose !" << endl; q++;}
else if (a[0][2]==79 && a[1][1]==79 && a[2][0]==79)
{cout << "you lose !" << endl; q++;}
else
{cout << "tie!" << endl; t++;}
if (p == 1)
cout <<"\nUp to this point you have won:\t\t" << p << " game" << endl;
else cout <<"\nUp to this point you have won:\t\t" << p << " games" << endl;
if ( q == 1)
cout <<"The computer has won:\t\t\t" << q << " game" << endl;
else cout <<"The computer has won:\t\t\t" << q << " games" << endl;
if ( t == 1)
cout <<"And there have been:\t\t\t" << t << " tie\n" << endl;
else cout <<"And there have been:\t\t\t" << t << " ties\n" << endl;
cout << "Do you want to play again?(Y/N)" << endl;
cin >> answer;
}while (answer == 'y' || answer == 'Y');
getch();
return 0;
}
#include <iostream.h>
#include <conio.h>
#include <stdlib.h>
#include <iomanip.h>
#include <time.h>
#include <string>
int main(void)
{
int row,colomn,n,r,s,h,p,q,t,m;
char a[3][3],choice,answer;
p=0; q=0; t=0; // p: your wins, q: your loses, t: ties
cout << "\t\t 1 2 3\n\n";
cout << "\t\t\t| " << "|" << endl;
cout << "\t\t 1 | " << "|" << endl;
cout << "\t\t ---|---|---" << endl;
cout << "\t\t 2 | " << "|" << endl;
cout << "\t\t ---|---|---" << endl;
cout << "\t\t 3 | " << "|" << endl;
cout << "\t\t\t| " << "|\n\n" << endl;
cout << "Welcome to the game of TIC-TAC-TOE!\n\n";
cout << "Please enter your first name:\n\n";
string name;
cin >> name;
do
{
cout<<"\nWell "<<name<<", let's see who has the first move, me or you!\n";
cout<<"Choose! Head or Tail ?(H/T)\n";
cin >> choice;
srand(time(NULL));
h = rand()%2; // It produce 0 or 1. if it's 0 user has the
if (h == 0) // first move, else computer has it.
cout << "\nYou are first! " << endl;
else
cout << "\nI'm first, You're next!" << endl;
bool keep = true;
for (int i=0; i<3; i++)
for (int j=0; j<3; j++)
a[j] = '\0'; // all the matrix cells are blank now.
n=0;
do
{
if (h == 0)
{
cout << "\nIt's your turn " << name <<"!" << endl;
do
{
cout<<"\nPlease enter the number of the row you wish to place in:"<<endl;
cin >> row;
if (row > 3 || row < 1)
cout<<"\nCaution! you must enter only 1 or 2 or 3 for the row\n";
}while(row > 3 || row < 1);
do
{
cout<<"Please enter the number of the colomn you wish to place in:"<<endl;
cin >> colomn;
if (colomn > 3 || colomn < 1)
cout<<"\nCaution! you must enter only 1 or 2 or 3 for the colomn\n";
}while (colomn > 3 || colomn < 1);
row = row - 1; // because in c++ arrays start from 0 not 1.
colomn = colomn - 1;
if (n>0 && a[row][colomn] != '\0')
{
do // checks that the cell is blank or not.
{
cout << "\nThis place is full ! select another\n" << endl;
cout<<"Please enter the number of the row you wish to place in:" << endl;
cin >> row;
cout<<"Please enter the number of the colomn you wish to place in:"<<endl;
cin >> colomn;
row = row - 1;
colomn = colomn - 1;
}while(a[row][colomn] != '\0');
}
a[row][colomn] = 88; // 88 is the number that produce X as a char.
cout<<"\t\t 1 2 3\n\n";
cout<<"\t\t\t| " << "|" << endl;
cout<<"\t 1 "<<a[0][0]<<" | "<<a[0][1]<<" | "<<a[0][2]<<endl;
cout<<"\t\t ---|---|---" << endl;
cout<<"\t 2 "<<a[1][0]<<" | "<<a[1][1]<<" | "<<a[1][2]<<endl;
cout<<"\t\t ---|---|---" << endl;
cout<<"\t 3 "<<a[2][0]<<" | "<<a[2][1]<<" | "<<a[2][2]<<endl;
cout<<"\t\t\t| " << "|\n" << endl;
if (a[0][0]==88 && a[0][1]==88 && a[0][2]==88) keep = false;
if (a[1][0]==88 && a[1][1]==88 && a[1][2]==88) keep = false;
if (a[2][0]==88 && a[2][1]==88 && a[2][2]==88) keep = false;
if (a[0][0]==88 && a[1][0]==88 && a[2][0]==88) keep = false;
if (a[0][1]==88 && a[1][1]==88 && a[2][1]==88) keep = false;
if (a[0][2]==88 && a[1][2]==88 && a[2][2]==88) keep = false;
if (a[0][0]==88 && a[1][1]==88 && a[2][2]==88) keep = false;
if (a[0][2]==88 && a[1][1]==88 && a[2][0]==88) keep = false;
}
m=0;
for (int i=0; i<3; i++)
for (int j=0; j<3; j++)
if (a[j] != '\0')
m++;
if (m == 9) // if m become 9 it means that all 9 cells are full
keep = false; // thus the game is tie and program should go out
// of the loop by making keep false.
if (keep == true)
{
do
{
r = rand()%3; // will produce 0 or 1 or 2.
s = rand()%3;
}while(a[r]
a[r]
cout << "Press any key for the computer's move \n\n";
getch();
cout<<"The computer placed an O in location "<<(r+1)<<","<<(s+1)<< endl;
cout<<"\n\t\t 1 2 3\n\n";
cout<<"\t\t\t| " << "|" << endl;
cout<<"\t 1 "<<a[0][0]<<" | "<<a[0][1]<<" | "<<a[0][2]<<endl;
cout<<"\t\t ---|---|---" << endl;
cout<<"\t 2 "<<a[1][0]<<" | "<<a[1][1]<<" | "<<a[1][2]<<endl;
cout<<"\t\t ---|---|---" << endl;
cout<<"\t 3 "<<a[2][0]<<" | "<<a[2][1]<<" | "<<a[2][2]<<endl;
cout<<"\t\t\t| " << "|\n" << endl;
}
if (a[0][0]==79 && a[0][1]==79 && a[0][2]==79) keep = false;
if (a[1][0]==79 && a[1][1]==79 && a[1][2]==79) keep = false;
if (a[2][0]==79 && a[2][1]==79 && a[2][2]==79) keep = false;
if (a[0][0]==79 && a[1][0]==79 && a[2][0]==79) keep = false;
if (a[0][1]==79 && a[1][1]==79 && a[2][1]==79) keep = false;
if (a[0][2]==79 && a[1][2]==79 && a[2][2]==79) keep = false;
if (a[0][0]==79 && a[1][1]==79 && a[2][2]==79) keep = false;
if (a[0][2]==79 && a[1][1]==79 && a[2][0]==79) keep = false;
h = 0;
n++;
}while(n < 5 && keep);
cout << "finish! last step is :" << endl;
cout << "\t\t\t| " << "|" << endl;
cout << "\t\t "<<a[0][0]<<" | "<<a[0][1]<<" | "<<a[0][2]<<endl;
cout << "\t\t ---|---|---" << endl;
cout << "\t\t "<<a[1][0]<<" | "<<a[1][1]<<" | "<<a[1][2]<<endl;
cout << "\t\t ---|---|---" << endl;
cout << "\t\t "<<a[2][0]<<" | "<<a[2][1]<<" | "<<a[2][2]<<endl;
cout << "\t\t\t| " << "|" << endl;
if (a[0][0]==88 && a[0][1]==88 && a[0][2]==88)
{cout << "you win !" << endl; p++;}
else if (a[1][0]==88 && a[1][1]==88 && a[1][2]==88)
{cout << "you win !" << endl; p++;}
else if (a[2][0]==88 && a[2][1]==88 && a[2][2]==88)
{cout << "you win !" << endl; p++;}
else if (a[0][0]==88 && a[1][0]==88 && a[2][0]==88)
{cout << "you win !" << endl; p++;}
else if (a[0][1]==88 && a[1][1]==88 && a[2][1]==88)
{cout << "you win !" << endl; p++;}
else if (a[0][2]==88 && a[1][2]==88 && a[2][2]==88)
{cout << "you win !" << endl; p++;}
else if (a[0][0]==88 && a[1][1]==88 && a[2][2]==88)
{cout << "you win !" << endl; p++;}
else if (a[0][2]==88 && a[1][1]==88 && a[2][0]==88)
{cout << "you win !" << endl; p++;}
else if (a[0][0]==79 && a[0][1]==79 && a[0][2]==79)
{cout << "you lose !" << endl; q++;}
else if (a[1][0]==79 && a[1][1]==79 && a[1][2]==79)
{cout << "you lose !" << endl; q++;}
else if (a[2][0]==79 && a[2][1]==79 && a[2][2]==79)
{cout << "you lose !" << endl; q++;}
else if (a[0][0]==79 && a[1][0]==79 && a[2][0]==79)
{cout << "you lose !" << endl; q++;}
else if (a[0][1]==79 && a[1][1]==79 && a[2][1]==79)
{cout << "you lose !" << endl; q++;}
else if (a[0][2]==79 && a[1][2]==79 && a[2][2]==79)
{cout << "you lose !" << endl; q++;}
else if (a[0][0]==79 && a[1][1]==79 && a[2][2]==79)
{cout << "you lose !" << endl; q++;}
else if (a[0][2]==79 && a[1][1]==79 && a[2][0]==79)
{cout << "you lose !" << endl; q++;}
else
{cout << "tie!" << endl; t++;}
if (p == 1)
cout <<"\nUp to this point you have won:\t\t" << p << " game" << endl;
else cout <<"\nUp to this point you have won:\t\t" << p << " games" << endl;
if ( q == 1)
cout <<"The computer has won:\t\t\t" << q << " game" << endl;
else cout <<"The computer has won:\t\t\t" << q << " games" << endl;
if ( t == 1)
cout <<"And there have been:\t\t\t" << t << " tie\n" << endl;
else cout <<"And there have been:\t\t\t" << t << " ties\n" << endl;
cout << "Do you want to play again?(Y/N)" << endl;
cin >> answer;
}while (answer == 'y' || answer == 'Y');
getch();
return 0;
}
از اونجا که من مبتدی هستم! یه چیزی حدود 200 تا If , cout داره فکر کنم خیلی کوتاه تر از اینم میشد نوشت.
فقط امیدوارم بچه های کلاس اینجا رو پیدا نکنن و سورسه من لو نره
نمایی از بازی!! :
]
خود بازی و سورسش :