Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /* ヘッダファイルのインクルード */
- #include <stdio.h>
- /* プロトタイプ宣言 */
- int a(int s[3][3],int x,int y,int c);
- int b(int s[3][3],int c);
- int main(void)
- {
- /* 変数宣言 */
- int i,j,x,y,r,s[3][3];
- /* ボード初期化 */
- for(i=0;i<3;i++){
- for(j=0;j<3;j++)s[i][j]=0;
- }
- /* ボード最初の表示 */
- a(s,0,0,0);
- /* 9回繰り返し */
- for(i=1;i<10;i++){
- /* 先手入力 */
- printf("\n先手、入力してください。\n");
- printf("X:");
- scanf("%d",&x);
- printf("Y:");
- scanf("%d",&y);
- /* 入力した手でボード表示 */
- a(s,x,y,1);
- /* 先手の勝ちが決まったかを判定 */
- r=b(s,1);
- if(r!=0){
- printf("\n先手の勝ちです!\n");
- return 0;
- }
- /* 後手入力 */
- printf("\n後手入力してください。\n");
- printf("X:");
- scanf("%d",&x);
- printf("Y:");
- scanf("%d",&y);
- /* 入力した手でボード表示 */
- a(s,x,y,2);
- /* 後手の勝ちが決まったかを判定 */
- r=b(s,2);
- if(r!=0){
- printf("\n後手の勝ちです!\n");
- return 0;
- }
- }
- printf("\n引き分けです!\n");
- return 0;
- }
- /* ゲームボード表示 */
- int a(int s[3][3],int x,int y,int c){
- /* 変数宣言 */
- int i,j;
- /* 1~3を0~2に変える */
- x=x-1;
- y=y-1;
- if(s[y][x]==0){
- /* 空いているところならば */
- /* 先手だったら1を後手だったら2を入れる */
- if(c==1)s[y][x]=1;
- if(c==2)s[y][x]=2;
- }
- else{
- /* 空いていないところならば */
- printf("\n 勝負!\n");
- }
- /* ボード表示 */
- printf("* 1 2 3 <X\n");
- for(i=0;i<3;i++){
- for(j=0;j<3;j++){
- if(j==0) printf("%d ",i+1);
- printf("%d ",s[i][j]);
- }
- printf("\n");
- }
- printf("^\nY\n");
- return 0;
- }
- /* 勝ちが決定したかどうかを判定 */
- int b(int s[3][3],int c){
- int f;
- f=0;
- /* 横一列がcになっているかどうか */
- if(s[0][0]==c&&s[0][1]==c&&s[0][2]==c)f=c;
- if(s[1][0]==c&&s[1][1]==c&&s[1][2]==c)f=c;
- if(s[2][0]==c&&s[2][1]==c&&s[2][2]==c)f=c;
- /* 縦一列がcになっているかどうか */
- if(s[0][0]==c&&s[1][0]==c&&s[2][0]==c)f=c;
- if(s[0][1]==c&&s[1][1]==c&&s[2][1]==c)f=c;
- if(s[0][2]==c&&s[1][2]==c&&s[2][2]==c)f=c;
- /* 斜めがcになっているかどうか */
- if(s[0][0]==c&&s[1][1]==c&&s[2][2]==c)f=c;
- if(s[0][2]==c&&s[1][1]==c&&s[2][0]==c)f=c;
- /* 勝っている場合はcが戻り値として返る */
- return f;
- }
Advertisement
Add Comment
Please, Sign In to add comment