學(xué)習(xí)c++也有一段日子了,從毫無(wú)頭緒到現(xiàn)在剛剛?cè)腴T(mén)已經(jīng)過(guò)去將近一年了。不知是學(xué)校老師太差還是自己不夠積極,總覺(jué)得毫無(wú)長(zhǎng)進(jìn)!常在網(wǎng)上加些同樣不想在學(xué)校僅僅是混個(gè)畢業(yè)的朋友,沒(méi)事討論討論(身邊大多都是想混混日子的)。
井字游戲其實(shí)原理相當(dāng)簡(jiǎn)單,九個(gè)格子,不管橫豎還對(duì)角線上的三個(gè)子全是一方的,這方九贏的game。
#include<iostream>
using namespace std;
class line{
private:
char box[9];
public:
int step,pps,temp; //標(biāo)志量控制條件
int setline(){ //構(gòu)造函數(shù)用于下棋過(guò)程中顯示每個(gè)位置的數(shù)字,方便弈者選擇
for(int i=0;i<9;++i)
box[i]=i+'1'; //將數(shù)字的ascii碼放入數(shù)組,這樣解決了char類(lèi)型數(shù)組中放數(shù)字的矛盾
return(0);
}
int check(int);
int showTable();
int showChange();
int winLost();
int continueAbort();
};
//用于每次開(kāi)盤(pán)時(shí)顯示棋盤(pán)
int line::showTable(){
cout<<" *************"<<endl;
cout<<" | 1 | 2 | 3 |"<<endl;
cout<<" -------------"<<endl;
cout<<" | 4 | 5 | 6 |"<<endl;
cout<<" -------------"<<endl;
cout<<" | 7 | 8 | 9 |"<<endl;
cout<<" *************"<<endl;
return(0);
}
//每步棋盤(pán)上棋子的變化
int line::showChange(){
int num=0;
cin>>num;
--num;
while(box[num]=='a'||box[num]=='b'||num<0||num>8){
cout<<"\nThis number have been chosen!please take another one: ";
cin>>num;
--num;
}
if(step==1)box[num]='a';
else box[num]='b';
cout<<"\n *************"<<endl;
for(int i=0,j=0;j<3;++j){
cout<<" | "<<box[i]<<" | "<<box[i+1]<<" | "<<box[i+2]<<" |"<<endl;
i=i+3;
cout<<" *************"<<endl;}
winLost(); // 每次棋盤(pán)變化判斷一下輸贏
return(0);
}
//判斷輸贏條件
int line::check(int ch){
return((box[0]+box[1]+box[2]==ch||
box[0]+box[3]+box[6]==ch||
box[0]+box[4]+box[8]==ch||
box[3]+box[4]+box[5]==ch||
box[7]+box[8]+box[6]==ch||
box[2]+box[4]+box[6]==ch||
box[1]+box[4]+box[7]==ch||
box[2]+box[8]+box[5]==ch));
}
// 顯示輸贏結(jié)果,設(shè)置pps跳出單次下棋過(guò)程,即有一方贏了就結(jié)束該輪比賽
int line::winLost(){
if(check(3*'a')){
pps=1;
cout<<"player a win the game"<<endl;
}
else if(check(3*'b')){
pps=1;
cout<<"player b win the game"<<endl;
}
return(0);
}
// 再來(lái)一盤(pán)
int line::continueAbort(){
char pos;
cout<<"press c to continue_press anykey to abort"<<endl;
cin>>pos;
if(pos=='c'){
pps=0;
temp=0;
showTable();
setline();
return (1);
}
else return(0);
}
int main(){
line player;
player.setline();
cout<<"let us play the game"<<endl;
player.showTable();
do
{
for( int i=0;i<9;++i){
if(i%2==0){
player.step=1;
cout<<"\nplayer a,please choose the number you fit: ";
}
else{
player.step=0;
cout<<"\nplayer b,please choose the number you fit: ";
}
player.showChange();
if(player.pps==1)break;
player.temp=i;
}
if(player.temp==8){
cout<<"no one win the game!"<<endl;
break;
}
}while(player.continueAbort());
return (0);
}
修修補(bǔ)補(bǔ)終于完工,但總讓我感覺(jué)很差勁:
1.能否減少函數(shù)
2.winLost( )能簡(jiǎn)化嗎
3.能使所有變量step和pps放在私有里嗎
4.showChange()內(nèi)調(diào)用winLost()導(dǎo)致每下一個(gè)子顯示no one win the game!
ps1:又改了一下,解決了第4個(gè)問(wèn)題。不過(guò)又增加了一個(gè)標(biāo)志量和一個(gè)函數(shù)!有必要減少函數(shù)嗎?其他問(wèn)題估計(jì)不能解決,除非重新寫(xiě)了。就算完成了吧!
ps2:鑒于評(píng)論添加了一些注釋?zhuān)敬蛩阕约簩W(xué)學(xué)而已,沒(méi)想到還有人關(guān)注,頗有些驚喜
4月26日想做個(gè)人機(jī)對(duì)弈,發(fā)現(xiàn)了原來(lái)代碼里平局有錯(cuò)誤,遂將temp=9 改成8,這樣就解決了平局問(wèn)題