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



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

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