C++언어 로 즐 거 운 소 거 를 실현 합 니 다.

5319 단어 C++소소 하 다
본 논문 의 사례 는 여러분 에 게 C++즐 거 운 소 락 을 실현 하 는 구체 적 인 코드 를 공유 하여 여러분 께 참고 하 시기 바 랍 니 다.구체 적 인 내용 은 다음 과 같 습 니 다.
C++로 이 루어 진 즐거움 소 거 는 주로 하나의 모듈 로 나 뉘 어 이 루어 집 니 다.코드 의 결합 성 이 적 습 니 다.여기 서 xiaoxiaogame 류 를 사용 하여 이 루어 집 니 다.그 중에서 구조 함수 에서 배열 과 변수 에 대한 초기 화 xiaoxiaogame(int row 1,int col 1);void display()사용 하기;이러한 함 수 는 bool isvalid(int x,int y)로 표 시 됩 니 다.좌표 가 있 는 위 치 를 없 앨 수 있 는 지 판단 하기 위해 bool isgameover()를 사용 합 니 다.게임 이 끝 났 는 지 판단 하려 면 void remove(int x,int y,int target)를 사용 하 십시오.사각형 을 제거 한 다음 에 void adjustment()로 사각형 을 제거 한 위 치 를 void play game()으로 디 버 깅 합 니 다.게임 을 수행 하 겠 습 니 다.
코드 는 다음 과 같 습 니 다:

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

class xiaoxiaogame
{
public:
 //               
 xiaoxiaogame(int row1, int col1);
 //  
 void display();
 //               
 bool isvalid(int x, int y);
 //         
 bool isgameover();
 //            
 void remove(int x, int y, int target);
 //                 
 void adjustment();
 //    
 void playgame();
private:
 //              
 vector<vector<int>>nums;
 //       
 vector<vector<bool>>state;
 //    
 int score;
 //            
 int cnt;
 //       
 int row;
 //       
 int col;
};
xiaoxiaogame::xiaoxiaogame(int row1, int col1)
{
 row = row1;
 col = col1;
 score = 0;
 cnt = 0;
 srand(time(0));
 vector<vector<int>>tmp(row1,vector<int>(col1,0));
 vector<vector<bool>>temp(row1, vector<bool>(col1, false));
 state = temp;
 for (int i = 0; i < row; i++)
 {
 for (int j = 0; j < col; j++)
 {
  tmp[i][j] = rand() % 3;
 }
 }
 nums = tmp;
 display();
}
void xiaoxiaogame::display()
{
 for (int i = 0; i < row; i++)
 {
 for (int j = 0; j < col; j++)
 {
  if (!state[i][j])
  cout << nums[i][j] << " ";
  else cout << " ";
 }
 cout << endl;
 }
 cout << "your score is :" << score << endl;
}
bool xiaoxiaogame::isvalid(int x, int y)
{
 if (x < 0 || x >= row || y < 0 || y >= col || state[x][y])return false;
 return true;
}
bool xiaoxiaogame::isgameover()
{
 for (int i = 0; i < row; i++)
 {
 for (int j = 0; j < col; j++)
 {
  int target = nums[i][j];
  int x = i;
  int y = j;
  if (!isvalid(i, j))return false;
  if ((isvalid(x + 1, y) && nums[x + 1][y] == target) || (isvalid(x - 1, y) && nums[x - 1][y] == target) || \
  (isvalid(x, y + 1) && nums[x][y + 1] == target) || (isvalid(x, y - 1) && nums[x][y - 1] == target))
  return false;
 }
 }
 return true;
}
void xiaoxiaogame::remove(int x, int y, int target)
{
 if (!isvalid(x, y))return;
 if (nums[x][y] != target)return;
 state[x][y] = true;
 cnt++;
 remove(x + 1, y, target);
 remove(x - 1, y, target);
 remove(x, y + 1, target);
 remove(x, y - 1, target);
}
void xiaoxiaogame::adjustment()
{
 for (int j = 0; j < col; j++)
 {
 vector<int>tmp;
 for (int i = row - 1; i >= 0; --i)
 {
  if (!state[i][j])tmp.push_back(nums[i][j]);

 }
 int r = row - 1;
 for (int i = 0; i < tmp.size(); i++)
 {
  nums[r][j] = tmp[i];
  state[r][j] = false;
  r--;
 }
 for (; r >= 0; r--)
 {
  state[r][j] = true;
 }
 }
}
void xiaoxiaogame::playgame()
{
 int x, y;
 while (cin >> x >> y)
 {
 if (!isvalid(x, y))continue;
 int target = nums[x][y];
 cnt = 0;
 if ((isvalid(x + 1, y) && nums[x + 1][y] == target) || (isvalid(x - 1, y) && nums[x - 1][y] == target) || \
  (isvalid(x, y + 1) && nums[x][y + 1] == target) || (isvalid(x, y - 1) && nums[x][y - 1] == target))
  remove(x, y, target);
 score += target*cnt;
 adjustment();
 display();
 if (isgameover())
 {
  cout << "gameover" << endl;
  break;
 }
 }
}
int main()
{
 xiaoxiaogame t(10, 10);
 t.playgame();
 cin.get();
 return 0;
}
더 많은 재 미 있 는 클래식 게임 을 통 해 주 제 를 실현 하고 여러분 에 게 공유 합 니 다.
C++클래식 게임 모음
python 클래식 게임 모음
python 러시아 블록 게임 집합
JavaScript 클래식 게임 을 계속 합 니 다.
javascript 고전 게임 모음
이상 이 바로 본 고의 모든 내용 입 니 다.여러분 의 학습 에 도움 이 되 고 저 희 를 많이 응원 해 주 셨 으 면 좋 겠 습 니 다.

좋은 웹페이지 즐겨찾기