| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 572 人关注过本帖
标题:一个适合新手, 简单易懂的进子棋游戏代码
取消只看楼主 加入收藏
BuilderZ
Rank: 3Rank: 3
等 级:论坛游侠
威 望:1
帖 子:82
专家分:123
注 册:2014-9-22
结帖率:50%
收藏
 问题点数:0 回复次数:1 
一个适合新手, 简单易懂的进子棋游戏代码
#include <iostream>
#include <stdlib.h>
using namespace std;
class CGame
{
public:
    CGame();
    void Print();
    int Cal(int i, int j);
    bool Judge(int i, int j, int k);
    void Play();
private:
    char status[9];
    int score[9];
    int fail, counter;
};
CGame::CGame()
{
    for (int i = 0; i < 9; i++)
    {
        status[i] = '1' + i;
    };
    counter = 0;
    fail = 0;
};
void CGame::Print()
{
    system("cls");
    cout << " " << status[0] << " | " << status[1] << " | " << status[2] << endl;
    cout << "--+--+--+--+--+--" << endl;
    cout << " " << status[3] << " | " << status[4] << " | " << status[5] << endl;
    cout << "--+--+--+--+--+--" << endl;
    cout << " " << status[6] << " | " << status[7] << " | " << status[8] << endl;
};
int CGame::Cal(int i, int j)
{
    int result = 0;
    if ((status[i - 1] == 'X') && (status[j - 1] == 'X'))
    {
        result = 50;
        fail = 2;
    }
    else if ((status[i - 1] == 'O') && (status[j - 1] == 'O'))
    {
        result = 25;
    }
    else if (((status[i - 1] == 'X') && (status[j - 1] < 'A')) || ((status[i - 1] < 'A') && (status[j - 1] == 'X')))
    {
        result = 10;
    }
    else if (((status[i - 1] == 'O') && (status[j - 1] < 'A')) || ((status[i - 1] < 'A') && (status[j - 1] == 'O')))
    {
        result = 4;
    };
    return result;
};
bool CGame::Judge(int i, int j, int k)
{
    return ((status[i - 1] == 'O') && (status[j - 1] == 'O') && (status[k - 1] == 'O'));
};
void CGame::Play()
{
    int n = 0, max = 0, temp = 0;
    char ch;
    Print();
    cout <<"Choose: Start First(input F) or Second(input S)" << endl;
    cin >> ch;
    if ((ch == 's') && (ch == 'S'))
    {
        status[4] = 'X';
        counter++;
        Print();
        cout << "Computer Put at NO.5." << endl;
    };
    do
    {
        do
        {
            cout << "Please Put on (1 - 9)." << endl;
            cin >> n;
        }while (status[n - 1] >= 'A');
        status[n - 1] = 'O';
        counter++;
        Print();
        if (Judge(1, 2, 3) || Judge(4, 5, 6) || Judge(7, 8, 9) || Judge(1, 4, 7) || Judge(2, 5, 8) || Judge(3, 6, 9) || Judge(1, 5, 9) || Judge(3, 5, 7))
        {
            fail = 1;
        };
        if ((fail < 1) && (counter < 9))
        {
            for (int k = 0; k < 9; k++)
                {
                if (status[k] < 'A')
                    {
                     switch (k)
                     {
                     case 0: score[0] = Cal(2, 3) + Cal(4, 7) + Cal(5, 9);
                         break;
                    case 1: score[1] = Cal(1, 3) + Cal(5, 8);
                         break;
                    case 2: score[2] = Cal(1, 2) + Cal(6, 9) + Cal(5, 7);
                        break;
                    case 3: score[3] = Cal(5, 6) + Cal(1, 7);
                             break;
                     case 4: score[4] = Cal(4, 6) + Cal(2, 8) + Cal(1, 9) + Cal(3, 7);
                         break;
                     case 5: score[5] = Cal(4, 5) + Cal(3, 9);                        
                         break;
                     case 6: score[6] = Cal(8, 9) + Cal(1, 4) + Cal(3, 5);
                         break;
                     case 7: score[7] = Cal(7, 9) + Cal(2, 5);
                        break;
                    case 8: score[8] = Cal(7, 8) + Cal(3, 6) + Cal(1, 5);
                        break;
                    };
                }
                else
                {
                    score[k] = -1;
                };
            };
        };        
        max = score[0];
        for (int k = 1; k < 9; k++)
        {
            if (score[k] > max)
            {
                max = score[k];
                temp = k;
            };
        };
        status[temp] = 'X';
        counter++;
        cout << "Computer Plays " << temp + 1 << "." << endl;
        Print();
    }while ((fail <= 0) && (counter < 9));   
    if (fail == 0)
    {
        cout << "Your Draw." << endl;
    }
    else if (fail == 1)
    {
        cout << "You Won." << endl;
    }
    else
    {
        cout << "You Lose." << endl;
    };
};
|
|
|
|
|
#include "CGame.h"
int main()
{
    int a;
    CGame game;
    game.Play();
    return 0;
    cout << "Press Any Key To End And Enter." << endl;
    cin >> a;
};
搜索更多相关主题的帖子: include 水龙头 游戏 管道 健康 
2015-04-02 02:26
BuilderZ
Rank: 3Rank: 3
等 级:论坛游侠
威 望:1
帖 子:82
专家分:123
注 册:2014-9-22
收藏
得分:0 
回复 2楼 诸葛欧阳
google翻译上打不出来那个进
2015-04-10 05:33
快速回复:一个适合新手, 简单易懂的进子棋游戏代码
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.025720 second(s), 8 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved