| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 3124 人关注过本帖
标题:简单井字棋判断输赢, 菜鸟程序求纠正改错
只看楼主 加入收藏
cyy06180521
Rank: 1
等 级:新手上路
帖 子:36
专家分:0
注 册:2015-8-21
结帖率:100%
收藏
已结贴  问题点数:20 回复次数:6 
简单井字棋判断输赢, 菜鸟程序求纠正改错
想做一个不完全的井字棋算法,只需一次性输入所有棋子,判断输赢
下面我的程序,应该有好多错误,但是不知道怎么改,希望有大神来纠正,谢谢
老是显示 warning: 'winner' is used uninitialized in this function [-Wuninitialized]

而且不管棋子怎么摆,最后结果都是No winner

程序代码:
#include <stdio.h>

#define TRUE     1
#define FALSE    0

#define SIZE     3

#define NONE    -1
#define NOUGHTS  0
#define CROSSES  1
#define EMPTY    2

void  scanBoard( int board[SIZE][SIZE] );
void printBoard( int board[SIZE][SIZE] );
int   getWinner( int board[SIZE][SIZE] );

int main( void )
{
    int board[SIZE][SIZE];
    int winner;

    printf("Please enter the board:\n");
    scanBoard( board );
   
    printf("Here is the board:\n");
    printBoard( board );
    
    getWinner( board ); // 输出最终结果
    if(winner == CROSSES) {
        printf("CROSSES win");
    }
    else if(winner == NOUGHTS) {
        printf("NOUGHTS win");
    }
    else {
        printf("No winner");
    }  
    printf("\n");
              
    return 0;
}


void scanBoard(int board[SIZE][SIZE]) {  // 直接输入棋子 例如: 1 2 1
    int i, j;                                                  2 0 1 
    for(i = 0; i < SIZE; i++) {                                0 0 1
        for(j = 0; j < SIZE; j++) {
            scanf("%d", &board[i][j]); 
        }
    }
    
    return;
}            


void printBoard( int board[SIZE][SIZE] ) {  // 输出最后棋盘, 例如:  X . X
    int i, j;                                                       . O X
    for(i = 0; i < SIZE; i++) {                                     O O X  
        for(j = 0; j < SIZE; j++) {  
            if(board[i][j] == NOUGHTS) {                
                printf(" O");
            }
            else if(board[i][j] == CROSSES) {
                printf(" X");
            }
            else {
                printf(" .");
            }                
        }
        printf("\n");
    }  
    printf("\n");    
    return;
}               


int getWinner( int board[SIZE][SIZE] ) {
    
    int winner;
    int i, j;
        
    for(i = 0; i < SIZE; i++) {
        for(j = 0; j < SIZE; j++) {   
            if(board[0][0] != EMPTY && winner == NONE) {
                if(board[i][j] == board[i + 1][j]) {      // 判断一行
                    winner = board[0][j];
                }
                else if(board[i][j] == board[i][j + 1]) {   // 判断一列
                    winner = board[i][0];    
                }
                else if(board[i][j] == board[i + 1][j + 1]) {    //判断向右对角线
                    winner = board[0][0];
                }
                else if(board[i][SIZE - 1 - j] == board[i + 1][SIZE - 2 - j]) {   //判断向左对角线
                    winner = board[0][SIZE - 1];
                }
                return TRUE;
            }
            else {
                return FALSE;            
            }    
        }          
    }
    
    return winner;
}    
      


[ 本帖最后由 cyy06180521 于 2015-9-6 17:25 编辑 ]
搜索更多相关主题的帖子: 而且 
2015-09-06 17:23
lowrie
Rank: 3Rank: 3
等 级:论坛游侠
帖 子:81
专家分:138
注 册:2015-3-12
收藏
得分:10 
总共就8种情况,分别判断就可以了,还有就是要三个一样的才算赢


int getWinner( int board[SIZE][SIZE] ) {

    if(board[0][0]==0){
    }else{//判断第一行,第一列,对角线00-22
        if(board[0][1]==board[0][2]==board[0][0]||board[0][0]==board[1][0]==board[2][0]||board[0][0]==board[1][1]==board[2][2]){
            return board[0][0];
        }
    }

    if(board[1][0]==0){

    }else{//判断第二行
        if(board[1][0]==board[1][1]==board[1][2]){
            return board[1][0];
        }
    }
    if(board[2][0]==0){

    }else{//判断第三行 对角线20-02
        if(board[2][0]==board[2][1]==board[2][2]||board[2][0]==board[1][1]==board[0][2]){
            return board[2][0];
        }
    }



    if(board[0][1]==0){

    }else{//判断第二列
        if(board[0][1]==board[1][1]==board[2][1]){
            return board[0][1];
        }
    }

    if(board[0][2]==0){

    }else{//判断第三列
        if(board[0][2]==board[1][2]==board[2][2]){
            return board[0][2];
        }
    }

    return -1;//都判断完后返回无赢家
}
2015-09-07 13:10
cyy06180521
Rank: 1
等 级:新手上路
帖 子:36
专家分:0
注 册:2015-8-21
收藏
得分:0 
回复 3楼 边小白
恩,因为只是个小练习,所以就不用考虑这种情况..........
2015-09-07 20:32
cyy06180521
Rank: 1
等 级:新手上路
帖 子:36
专家分:0
注 册:2015-8-21
收藏
得分:0 
回复 2楼 lowrie
挺好,不过我是要求写对四子,五子....都适用的,anyway,谢谢你的程序
2015-09-07 20:33
cyy06180521
Rank: 1
等 级:新手上路
帖 子:36
专家分:0
注 册:2015-8-21
收藏
得分:0 
BTW,找到了答案.........
程序代码:
#include 

#define FALSE    0
#define TRUE     1

#define SIZE     3
#define NONE    -1
#define NOUGHTS  0
#define CROSSES  1
#define EMPTY    2

void  scanBoard( int board[SIZE][SIZE] );
void printBoard( int board[SIZE][SIZE] );
int   getWinner( int board[SIZE][SIZE] );

int main( void )
{
    int board[SIZE][SIZE];
    int winner;

    printf("Please enter the board:\n");
    scanBoard( board );
   
    printf("Here is the board:\n");
    printBoard( board );

    //Find and print the winner
    winner = getWinner( board );
    if( winner == NONE ) {
      printf("There is no winner\n");
    }
    else if( winner == NOUGHTS ) {
      printf("Noughts win\n");
    }
    else {
      printf("Crosses win\n");
    }    

    return 0;
}

void scanBoard( int board[SIZE][SIZE] ) {
  int r,c;
  for( r=0; r < SIZE; r++ ) {
    for( c=0; c < SIZE; c++ ) {
      scanf("%d",&board[r][c]);
    }
  }
}

void printBoard( int board[SIZE][SIZE] )
{
  int r,c;
  for( r=0; r < SIZE; r++ ) {
    for( c=0; c < SIZE; c++ ) {
      if( board[r][c] == NOUGHTS ) {
        printf(" O");
      }
      else if( board[r][c] == CROSSES ) {
        printf(" X");
      }
      else {
        printf(" .");
      }
    }
    printf("\n");
  }
  printf("\n");
}

int getWinner( int board[SIZE][SIZE] )
{
  int gameOver;
  int winner = NONE;
  int r,c;

  // check rows
  for( r = 0; r < SIZE && winner == NONE; r++ ) {
    gameOver = TRUE;
    for( c = 0; c < SIZE-1; c++ ) {
      if(   board[r][c] == EMPTY
     || board[r][c] != board[r][c+1]) {
        gameOver = FALSE;
      }
    }
    if( gameOver ) {
      winner = board[r][0];
    }
  }
  // check columns
  for( c = 0; c < SIZE && winner == NONE; c++ ) {
    gameOver = TRUE;
    for( r = 0; r < SIZE-1; r++ ) {
      if(   board[r][c] == EMPTY
     || board[r][c] != board[r+1][c]) {
        gameOver = FALSE;
      }
    }
    if( gameOver ) {
      winner = board[0][c];
    }
  }
  // check forward diagonal
  if( winner == NONE && board[0][0] != EMPTY ) {
    gameOver = TRUE;
    for( r = 0; r < SIZE-1; r++ ) {
      if( board[r][r] != board[r+1][r+1] ) {
        gameOver = FALSE;
      }
    }
    if( gameOver ) {
      winner = board[0][0];
    }
  }
  // check backward diagonal
  if( winner == NONE && board[0][SIZE-1] != EMPTY ) {
    gameOver = TRUE;
    for( r = 0; r < SIZE-1; r++ ) {
      if( board[r][SIZE-1-r] != board[r+1][SIZE-2-r]) {
        gameOver = FALSE;
      }
    }
    if( gameOver ) {
      winner = board[0][SIZE-1];
    }
  }

  return winner;
}
2015-09-07 20:35
林月儿
Rank: 16Rank: 16Rank: 16Rank: 16
来 自:湖南
等 级:版主
威 望:138
帖 子:2277
专家分:10647
注 册:2015-3-19
收藏
得分:0 
程序代码:
int x=0,y=0,z=0;
        boolean flag=false;
        for(int i=0;i<BOARD_SIZE;i++)
        {
            for(int j=0;j<BOARD_SIZE;j++)
            {
                for(int k=0;k<n;k++)
                {                    
                    if(!(board[(i+k)%BOARD_SIZE][j].equals("+")))
                    {
                        x++;
                    }
                    else
                    {
                        x=0;
                    }
                    if(!(board[i][(j+k)%BOARD_SIZE].equals("+")))
                    {
                        y++;
                    }
                    else
                    {
                        y=0;
                    }
                    if(!(board[(i+k)%BOARD_SIZE][(j+k)%BOARD_SIZE].equals("+")))
                    {
                        z++;
                    }
                    else
                    {
                        z=0;
                    }
                    if(x>n||y>n||z>n)
                    {
                        flag=true;
                    }
                }
            }

好像可以四合一,试试吧,没写完。。。。。

[ 本帖最后由 林月儿 于 2015-9-7 20:42 编辑 ]

剑栈风樯各苦辛,别时冰雪到时春
2015-09-07 20:41
快速回复:简单井字棋判断输赢, 菜鸟程序求纠正改错
数据加载中...
 
   



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

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