当日のOHP資料です。
次のファイルを、c:\usr\c\20131018に保存し、「全て展開」する。
オセロ盤を作成してみる
v1
#include <stdio.h> #define EMPTY '.' #define WHITE 'o' #define BLACK '*' #define BOARD_SIZE 8 typedef char BInfo; typedef struct { BInfo info[BOARD_SIZE][BOARD_SIZE]; } Board; int main(int argc, char *argv[] ) { Board board; int x; int y; for ( y = 0; y < BOARD_SIZE; y++ ) { for ( x = 0; x < BOARD_SIZE; x++ ) { board.info[x][y] = EMPTY; } } x = BOARD_SIZE/2; y = BOARD_SIZE/2; board.info[x][y] = WHITE; board.info[x-1][y-1] = WHITE; board.info[x][y-1] = BLACK; board.info[x-1][y] = BLACK; for ( y = 0; y < BOARD_SIZE; y++ ) { for ( x = 0; x < BOARD_SIZE; x++ ) { putchar ( board.info[x][y] ); } putchar ( '\n' ); } return 0; }
v2
#include <stdio.h> #define EMPTY ' ' #define WHITE 'o' #define BLACK '*' #define BOARD_SIZE 8 typedef char BInfo; typedef struct { BInfo info[BOARD_SIZE][BOARD_SIZE]; } Board; int main(int argc, char *argv[] ) { Board board; int x; int y; printf ( "Othello Version 2\n" ); for ( y = 0; y < BOARD_SIZE; y++ ) { for ( x = 0; x < BOARD_SIZE; x++ ) { board.info[x][y] = EMPTY; } } x = BOARD_SIZE/2; y = BOARD_SIZE/2; board.info[x][y] = WHITE; board.info[x-1][y-1] = WHITE; board.info[x][y-1] = BLACK; board.info[x-1][y] = BLACK; printf ( " A B C D E F G H\n" ); printf ( " +--+--+--+--+--+--+--+--+\n" ); for ( y = 0; y < BOARD_SIZE; y++ ) { printf ( "%d|", y+1 ); for ( x = 0; x < BOARD_SIZE; x++ ) { if ( board.info[x][y] == EMPTY ) { printf ( " " ); } else if ( board.info[x][y] == BLACK ) { printf ( "●" ); } else if ( board.info[x][y] == WHITE ) { printf ( "○" ); } /* putchar ( board.info[x][y] ); */ putchar ( '|' ); } putchar ( '\n' ); printf ( " +--+--+--+--+--+--+--+--+\n" ); } return 0; }
v3
#include <stdio.h> #define EMPTY ' ' #define WHITE 'o' #define BLACK '*' #define BOARD_SIZE 8 typedef char BInfo; typedef struct { BInfo info[BOARD_SIZE][BOARD_SIZE]; } Board; int main(int argc, char *argv[] ) { Board board; int x; int y; int color; printf ( "Othello Version 3\n" ); for ( y = 0; y < BOARD_SIZE; y++ ) { for ( x = 0; x < BOARD_SIZE; x++ ) { board.info[x][y] = EMPTY; } } x = BOARD_SIZE/2; y = BOARD_SIZE/2; board.info[x][y] = WHITE; board.info[x-1][y-1] = WHITE; board.info[x][y-1] = BLACK; board.info[x-1][y] = BLACK; while ( 1 == 1 ) { printf ( "コマを入力してください : \n" ); printf ( "x=" ); scanf ( "%d", &x ); printf ( "y=" ); scanf ( "%d", &y ); printf ( "color(B=0/W=1)=" ); scanf ( "%d", &color ); if ( color == 0 ) { board.info[x][y] = BLACK; } else { board.info[x][y] = WHITE; } printf ( " A B C D E F G H\n" ); printf ( " +--+--+--+--+--+--+--+--+\n" ); for ( y = 0; y < BOARD_SIZE; y++ ) { printf ( "%d|", y+1 ); for ( x = 0; x < BOARD_SIZE; x++ ) { if ( board.info[x][y] == EMPTY ) { printf ( " " ); } else if ( board.info[x][y] == BLACK ) { printf ( "●" ); } else if ( board.info[x][y] == WHITE ) { printf ( "○" ); } /* putchar ( board.info[x][y] ); */ putchar ( '|' ); } putchar ( '\n' ); printf ( " +--+--+--+--+--+--+--+--+\n" ); } } return 0; }
v4
#include <stdio.h> #define EMPTY ' ' #define WHITE 'o' #define BLACK '*' #define YOI 1 #define WARUI 0 #define BOARD_SIZE 8 typedef char BInfo; typedef struct { BInfo info[BOARD_SIZE][BOARD_SIZE]; } Board; int main(int argc, char *argv[] ) { Board board; int x; int y; int color; int input_color; int checkResult; /* うってよいかどうか (1=よい/0=悪い) */ int xx; int oc; printf ( "Othello Version 4\n" ); for ( y = 0; y < BOARD_SIZE; y++ ) { for ( x = 0; x < BOARD_SIZE; x++ ) { board.info[x][y] = EMPTY; } } x = BOARD_SIZE/2; y = BOARD_SIZE/2; board.info[x][y] = WHITE; board.info[x-1][y-1] = WHITE; board.info[x][y-1] = BLACK; board.info[x-1][y] = BLACK; while ( 1 == 1 ) { /* 1==1 は常に成立するのでずっと続く */ printf ( "コマを入力してください : \n" ); printf ( "x=" ); scanf ( "%d", &x ); printf ( "y=" ); scanf ( "%d", &y ); printf ( "color(B=0/W=1)=" ); scanf ( "%d", &input_color ); /* 入力(0/1) から、色(BLOCK/WHITE)への変換 */ if ( input_color == 0 ) { color = BLACK; } else { color = WHITE; } /* 打った手の右がひっくり返れば OK とする */ checkResult = YOI; /* とりあえず、チェック前なので、正しいとする */ if ( color == WHITE ) { oc = BLACK; } else { oc = WHITE; } /* x の正の方向 しかみない */ /* 本当ならば 8 方向をみる必要がある */ /* いきなり8 方向は大変なので、右方向だけやってみた */ if ( x < 0 || BOARD_SIZE <= x ) { /* 盤外なのでダメ */ checkResult = WARUI; } else if ( y < 0 || BOARD_SIZE <= y ) { checkResult = WARUI; } else if ( board.info[x][y] != EMPTY ) { /* すでにコマがある */ /* これは打てない */ checkResult = WARUI; } else if ( BOARD_SIZE - 1 <= x ) { /* 右端なので、その右はない */ checkResult = WARUI; } else if ( board.info[x+1][y] != oc ) { /* 打った右のコマが相手コマでなかった */ /* この場合も右のコマはひっくりかえらない */ checkResult = WARUI; } else { xx = x + 2; /* この位置に自分のコマがほしい */ while ( xx < BOARD_SIZE ) { if ( board.info[xx][y] == color ) { break; /* ここで while 終了 */ /* はさめたので OK */ } else if ( board.info[xx][y] == EMPTY ) { checkResult = WARUI; break; /* ダメ */ } else if ( board.info[xx][y] == oc ) { /* さらに次を見るまでワカラナイ*/ xx = xx + 1; } } /* while を抜けた時点で、 抜けた理由が3通りある 端まできた => checkState を 0(ダメ) にする必要がある はさめた => checkState を 1(ヨイ) のままで良い はさめなかった => checkState は 0(ダメ)になっている */ if ( xx >= BOARD_SIZE ) { /* 端までしらべた */ checkResult = WARUI; /* はさめなかった */ } } /* 指定された指し手が正しい場合だけ、 盤面を変化させる 具体的ははさんだコマはひっくり返す そうでない(正しくない指して)の場合は、 誤りであることを提示し、再入力させる */ if ( checkResult == WARUI ) { printf ( "そこには打てません\n" ); } else { int xxx; /* 引っくり返す場所 */ board.info[x][y] = color; /* うった場所 */ xxx = x + 1; /* 引っくり返す場所はその隣りから */ while ( xxx < xx ) { /* xx の直前迄 */ board.info[xxx][y] = color; /* はさんだコマをひっくり返す */ xxx = xxx + 1; /* 次の場所に進む */ } } printf ( " A B C D E F G H\n" ); printf ( " +--+--+--+--+--+--+--+--+\n" ); for ( y = 0; y < BOARD_SIZE; y++ ) { printf ( "%d|", y+1 ); for ( x = 0; x < BOARD_SIZE; x++ ) { if ( board.info[x][y] == EMPTY ) { printf ( " " ); } else if ( board.info[x][y] == BLACK ) { printf ( "●" ); } else if ( board.info[x][y] == WHITE ) { printf ( "○" ); } /* putchar ( board.info[x][y] ); */ putchar ( '|' ); } putchar ( '\n' ); printf ( " +--+--+--+--+--+--+--+--+\n" ); } } return 0; }
なし