五子棋

// gobang.c
#include <stdio.h>
#include <stdlib.h>
#include <conio.h>  // for getch()

#define LEN 21
int play1 = 1, play2 = 2; 
char chess[LEN][LEN];
unsigned int x = LEN/2, y = LEN/2;      // cursor Point
int count = 0;

void initGame();
void runGame();
void display();
int checkGame(char c);

int main(void)
{
    printf("Begin Game , Yes ?");
    if(getchar() == '\n'){
        initGame();
        runGame();
    }
    return 0;
}

void initGame()
{
    int i, j;
    for(i = 0; i < LEN; i++){

        for(j = 0; j < LEN; j++){
            if(i == 0 )
                chess[i][j] = 'a' + j-1;
            else if(j == 0)
                chess[i][j] = 'a' + i-1;
            else
                chess[i][j] = '.';
            chess[0][0] = 0;
        }
    }
    display();
}

void runGame()
{
    int input;
    while(1){
        input = getch();        
        if(input == 27)     //ESC
            exit(0);
        else if(input == 0xE0){     // control symbol for direction key
            input = getch();
            switch(input){
                case 0x48:  //up
                    y == 1 ? y = LEN-1 : y--; 
                    break;
                case 0x4B:  //left
                    x == 1 ? x = LEN-1 : x--;
                    break;
                case 0x4D:  //right
                    x == LEN-1 ? x = 0 : x++;
                    break;
                case 0x50:  //down
                    y == LEN-1 ? y = 0 : y++;
                    break;
            }   
        }
        else if(input == ' ' && chess[y][x] == '.'){    // space Laozi
            if(++count % 2 == play1){               
                chess[y][x] = 'O';
                if(checkGame('O') == 1){        // check 
                    display();
                    printf("Play1 with O Win !!!\n");
                    exit(0);
                }
            }
            else{
                chess[y][x] = 'X';
                if(checkGame('X') == 1){
                    display();
                    printf("Play2 with O Win !!!\n");
                    exit(0);
                }
            }
        }
        display();
    }
}

void display()
{
    int i, j;
    system("cls");
    for(i = 0; i < LEN; i++){
        for(j = 0; j < LEN; j++){
            printf("%-2c", chess[i][j]);
        }
        printf("\n");
    }
    if((count+1) % 2 == play1)
        printf("\nWhose Turn : Play1  O\t\t");
    else
        printf("\nWhose Turn : Play2  X\t\t");
    printf("P(x, y) = %c, %c\n", x+'a'-1, y+'a'-1);
}

int checkGame(char c)   // check last laozi
{
    int i, j, n = 1, stop1 = 0, stop2 = 0;
    for(i = 1; i <= 5; i++){        // x
        if(stop1 == 0 && x-i > 0 && chess[y][x-i] == c)
            n++;
        else
            stop1 = 1;
        if(stop2 == 0 && x+i < LEN && chess[y][x+i] == c)
            n++;
        else
            stop2 = 1;
        if (n >= 5)
            return 1;
        if(stop1 == 1 && stop2 == 1)
            break;  
    }

    stop1 = stop2 = 0, n = 1;
    for(i = 1; i <= 5; i++){        // y
        if(stop1 == 0 && y-i > 0 && chess[y-i][x] == c)
            n++;
        else
            stop1 = 1;
        if(stop2 == 0 && y+i < LEN && chess[y+i][x] == c)
            n++;
        else
            stop2 = 1;
        if (n >= 5)
            return 1;
        if(stop1 == 1 && stop2 == 1)
            break;  
    }

    stop1 = stop2 = 0, n = 1;
    for(i = 1; i <= 5; i++){    //x+1, y+1  --> x-1, y-1
        if(stop1 == 0 && x-i > 0 && y-i > 0 && chess[y-i][x-i] == c)
            n++;
        else
            stop1 = 1;
        if(stop1 == 0 && x+i < LEN && y+i < LEN && chess[y+i][x+i] == c)
            n++;
        else
            stop2 = 1;
        if(n >= 5)
            return 1;
        if(stop1 == 1 && stop2 == 1)
            break;
    }

    stop1 = stop2 = 0, n = 1;
    for(i = 1; i <= 5; i++){    //x+1, y-1  -->  x-1, y+1
        if(stop1 == 0 && x+i < LEN && y-i > 0 && chess[y-i][x+i] == c)
            n++;
        else
            stop1 = 1;
        if(stop1 == 0 && x-i > 0 && y+i < LEN && chess[y+i][x-i] == c)
            n++;
        else
            stop2 = 1;
        if(n >= 5)
            return 1;
        if(stop1 == 1 && stop2 == 1)
            break;
    }
    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值