BloggerAds

2011年6月7日 星期二

砲台遊戲

C++幫朋友寫的一個DOS小遊戲,兩個玩家在5*5方陣內自選砲台位置,互相猜,先猜到先贏

砲台GAME   


#include"Game.h"


int main(){
 
 cout<<"以下輸入方法,例如第一排(橫的)第二個(直的)請輸入1,2,皆為上到下左到右"<<endl;
 
 
 Game g1,g2;
 int c,r;
 bool answer=false;
 bool jud=false;//用來判斷是否重複猜
 g1.ReArray();
 g2.ReArray();
 while(1<2){
  cout<<"請玩家一輸入你炮台位置:"<<endl;
        cin>>g1;
  if((g1.c>0&&g1.c<6)&&(g1.r>0&&g1.r<6)){
   g1.SetArray();
   break;
  }else{
   cout<<"範圍1~5,請勿超出範圍"<<endl;
  }
  
 }
 system("CLS");
 while(1<2){
  cout<<"請玩家二輸入你炮台位置:"<<endl;
        cin>>g2;
  if((g2.c>0&&g2.c<6)&&(g2.r>0&&g2.r<6)){
   g2.SetArray();
   break;
  }else{
   cout<<"範圍1~5,請勿超出範圍"<<endl;
  }
  
 }
 system("CLS");
 for(;;){
  while(jud==false){
     cout<<"請玩家一猜玩家二的炮台位置:"<<endl;
     cin>>c;
     cin.ignore();
     cin>>r;
     jud=g2.Judge(c,r);
     if(jud==true){
      cout<<"這個位置猜過了啦 繼續猜吧"<<endl;
      jud=false;
     }else{
      break;
     }
  }
  answer=g2.Guess(c,r);
  if(answer==true){
   cout<<"恭喜阿!!玩家一猜中了WIN"<<endl;
   break;
  }else{
   cout<<"沒猜中喔 換下一個"<<endl;
  }
  while(jud==false){
     cout<<"請玩家二猜玩家一的炮台位置:"<<endl;
     cin>>c;
     cin.ignore();
     cin>>r;
     jud=g1.Judge(c,r);
     if(jud==true){
      cout<<"這個位置猜過了啦 繼續猜吧"<<endl;
         jud=false;
     }else{
      break;
     }
  }
  answer=g1.Guess(c,r);
  if(answer==true){
   cout<<"恭喜阿!!玩家二猜中了WIN"<<endl;
   break;
  }else{
   cout<<"沒猜中喔 換下一個"<<endl;
  }
 }
 
 system("pause");
 


};

#include<iostream>
#include<string>
using namespace std;

class Game{
  friend istream &operator>>(istream &in,Game &g){
    in>>g.c;
    in.ignore();
    in>>g.r;
    
    return in;
  }
public:
  int player[5][5];//0為沒選沒猜的,1為玩家選的,2為另一玩家猜但沒中的
  int c,r;//放橫,直的值
    void ReArray(){
      for(int i =0;i<5;i++){
         for(int j=0;j<5;j++){
          player[i][j]=0; 
        }
     }
    }
    void PrintArray(){
    for(int i =0;i<5;i++){
         for(int j=0;j<5;j++){
          cout<<player[i][j]<<","; 
        }
      cout<<endl;
     }
    }
    void SetArray(){
        player[c-1][r-1]=1;
    }
  bool Judge(int x,int y){//判斷
    if(player[x-1][y-1]==2)
      return true;
    return false;
  }
  bool Guess(int x,int y){
    if(x==c&&y==r)
      return true;
    player[x-1][y-1]=2;
    return false;
  }
   
};