最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

C++實(shí)現(xiàn)五子棋小程序

 更新時(shí)間:2022年05月05日 11:49:08   作者:Mind-Explorer  
這篇文章主要為大家詳細(xì)介紹了C++實(shí)現(xiàn)五子棋游戲,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下

這是一個(gè)用C++寫的五子棋的小程序,關(guān)于A若是占據(jù)了已經(jīng)下了的位置處理的不好。改動(dòng) hight,與width ,與q[][] 可以將棋盤擴(kuò)大。

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

class qipan
{
public:
 qipan() {}
 ~qipan() {};


 //向上下左右,斜的方向
 char left(int x, int y)
 {//檢查是否合適
 if (x >= 1 && x <= hight&& y - 1 >= 1 && y - 1 <= width)
 {
  return q[x][y - 1];
 }
 else {
  return 'F';
 }

 }
 char right(int x, int y)
 {
 if (x >= 1 && x <= hight&&y + 1 >= 1 && y + 1 <= width)
 {
  return q[x][y + 1];
 }
 else {
  return 'F';
 }

 }
 char up(int x, int y)
 {
 if (x - 1 >= 1 && x - 1 <= hight && y >= 1 && y <= width)
 {
  return q[x - 1][y];
 }
 else {
  return 'F';
 }

 }
 char down(int x, int y)
 {
 if (x + 1 >= 1 && x + 1 <= hight && y >= 1 && y <= width)
 {
  return q[x + 1][y];
 }
 else {
  return 'F';
 }

 }
 char left_up(int x, int y)
 {
 if (x - 1 >= 1 && x - 1 <= hight && y - 1 >= 1 && y - 1 <= width)
 {
  return q[x - 1][y - 1];
 }
 else {
  return 'F';
 }

 }
 char left_down(int x, int y)
 {
 if (x + 1 >= 1 && x + 1 <= hight && y - 1 >= 1 && y - 1 <= width)
 {
  return q[x + 1][y - 1];
 }
 else {
  return 'F';
 }

 }
 char right_up(int x, int y)
 {
 if (x - 1 >= 1 && x - 1 <= hight && y + 1 >= 1 && y + 1 <= width)
 {
  return q[x - 1][y + 1];

 }
 else {
  return 'F';
 }


 }
 char right_down(int x, int y)
 {
 if (x + 1 >= 1 && x + 1 <= hight && y + 1 >= 1 && y + 1 <= width)
 {
  return q[x + 1][y + 1];
 }
 else {
  return 'F';
 }
 }

 void init_cur() {
 h_cur = hang;
 l_cur = lie;

 }
 bool win()
 {
 bool WIN = false;
 char temp = q[hang][lie];

 //以上為例,每次先看上面的5個(gè),假如一樣,iter++;否則 break;再加上下的方向,同樣iter++;最后iter+1==5,WIN=true;并且退出
 //各個(gè)方向重復(fù),上下左右,斜著的。
 //贏了直接退出。
 //顯示是誰贏了


 //左右
 int count_lr = 1;
 init_cur();
 for (int i = 0; i < 4; i++)
 {

  if (left(h_cur, l_cur) == temp)
  count_lr++;
  else
  break;
  l_cur--;

 }
 init_cur();
 for (int i = 0; i < 4; i++)
 {

  if (right(h_cur, l_cur) == temp)
  count_lr++;
  else
  break;
  l_cur++;

 }
 if (count_lr == 5)
  WIN = true;
 //上下
 int count_ud = 1;
 init_cur();
 for (int i = 0; i < 4; i++)
 {

  if (up(h_cur, l_cur) == temp)
  count_ud++;
  else
  break;
  h_cur--;

 }
 init_cur();
 for (int i = 0; i < 4; i++)
 {

  if (down(h_cur, l_cur) == temp)
  count_ud++;
  else
  break;
  h_cur++;

 }
 if (count_ud == 5)
  WIN = true;


 //左斜
 int count_lt = 1;
 init_cur();
 for (int i = 0; i < 4; i++)
 {

  if (left_up(h_cur, l_cur) == temp)
  count_lt++;
  else
  break;
  h_cur--;
  l_cur--;

 }
 init_cur();
 for (int i = 0; i < 4; i++)
 {

  if (left_down(h_cur, l_cur) == temp)
  count_lt++;
  else
  break;
  h_cur++;
  l_cur--;

 }
 if (count_lt == 5)
  WIN = true;

 //右邊斜
 int count_rt = 1;
 init_cur();
 for (int i = 0; i < 4; i++)
 {

  if (right_up(h_cur, l_cur) == temp)
  count_rt++;
  else
  break;
  h_cur--;
  l_cur++;

 }
 init_cur();
 for (int i = 0; i < 4; i++)
 {

  if (right_down(h_cur, l_cur) == temp)
  count_rt++;
  else
  break;
  h_cur++;
  l_cur++;

 }
 if (count_rt == 5)
  WIN = true;

 return WIN;
 }


 void qipan_array()
 {
 for (int i = 0; i < hight; i++) {
  for (int j = 0; j < width; j++)
  q[i][j] = '+';
 }
 }


 void prin_qipan()
 {
 //打印二維數(shù)組;每一行要打印上行號(hào),以及列號(hào)
 for (int i = 0; i <hight; i++)
 {

  for (int j = 0; j < width; j++)
  {
  cout << q[i][j] << " ";

  }
  cout << i;
  cout << endl;
 }

 for (int j = 0; j <width; j++)
 {
  cout << j << " ";
 }
 cout << endl << "________________________________" << endl;
 }
 int xiaqi(int player)
 {
 if (player == 1) {
  q[hang][lie] = '*';
 }
 else if (player == 2)
 {
  q[hang][lie] = '@';
 }
 else
  cout << "input player is wrong" << endl;
 return 0;
 }

 //初始化行列
 int gethang(int h)
 {

 hang = h;
 return 0;
 }
 int getlie(int l)
 {
 lie = l;
 return 0;
 }


 int h_cur;
 int l_cur;
 const int hight = 9;
 const int width = 9;
 int hang; int lie;
 char q[9][9];
};

int main()
{
 int hang, lie;
 qipan wzq;
 wzq.qipan_array();
 cout << "A 與B 玩五子棋" << endl;
 cout << "A use * and B use @" << endl;
 cout << "________________________________" << endl;
 for (int i = 0; i < 15; i++) {
 cout << "A 輸入行: ";
 cin >> hang;
 cout << "A 輸入列: ";
 cin >> lie;
 if (wzq.q[hang][lie] != '+')
  cout << "輸入的行列數(shù)字已經(jīng)有人占據(jù)了" << endl;
 else {
  wzq.gethang(hang);
  wzq.getlie(lie);

  wzq.xiaqi(1);
  wzq.prin_qipan();
  if (wzq.win())
  {
  cout << "A is winner" << endl;
  exit(0);
  }
 }
 //b 開始了
 cout << "B 輸入行: ";
 cin >> hang;
 cout << "B 輸入列: ";
 cin >> lie;
 if (wzq.q[hang][lie] != '+')
  cout << "輸入的行列數(shù)字已經(jīng)有人占據(jù)了" << endl;
 else {
  wzq.gethang(hang);
  wzq.getlie(lie);

  wzq.xiaqi(2);

  wzq.prin_qipan();
  if (wzq.win())
  {
  cout << "B is winner" << endl;
  exit(0);
  }
 }
 }
 return 0;


}

以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

最新評論

福海县| 图们市| 富裕县| 宁乡县| 大宁县| 盱眙县| 自治县| 庆城县| 吉隆县| 南和县| 沙河市| 阳春市| 洪雅县| 新宾| 乐亭县| 商都县| 临海市| 孙吴县| 望奎县| 青铜峡市| 阿鲁科尔沁旗| 崇左市| 黄陵县| 内江市| 桐城市| 潞城市| 隆子县| 伊川县| 济南市| 山西省| 汉源县| 台州市| 纳雍县| 濉溪县| 大港区| 什邡市| 桦南县| 新竹县| 河南省| 中超| 钟祥市|