简单井字棋判断输赢, 菜鸟程序求纠正改错
想做一个不完全的井字棋算法,只需一次性输入所有棋子,判断输赢下面我的程序,应该有好多错误,但是不知道怎么改,希望有大神来纠正,谢谢
老是显示 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 编辑 ]