Advertisement
Guest User

Untitled

a guest
Nov 26th, 2014
141
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.46 KB | None | 0 0
  1. //
  2. //  main.c
  3. //  3x3 OOXX
  4. //
  5. //  Created by eddie on 2014/11/26.
  6. //  Copyright (c) 2014年 eddie. All rights reserved.
  7. //
  8.  
  9. #include <stdio.h>
  10. #include <time.h>
  11. #include <stdlib.h>
  12. #include <string.h>
  13.  
  14. int main(int argc, const char * argv[]) {
  15.     int x,y,com;
  16.     char ox[9]={"*********"};
  17.     void OOXX(char*);
  18.     void checkWinner(char*);
  19.     char *tmp;
  20.     int index;
  21.     srand((unsigned)time(NULL));
  22.     while (1) {
  23.        
  24.         com = (rand() % 9);
  25.  
  26.         OOXX(ox); //印出井字
  27.        
  28.         scanf("%d,%d",&x,&y); //玩家
  29.         x= (x==0)?  x+y :  x * 3 + y ;
  30.        
  31.         while ( ox[x] != '*' ){ //玩家重複
  32.             printf("重複\n");
  33.             OOXX(ox);  //印出井字
  34.             scanf("%d,%d",&x,&y); //再次輸入
  35.             x= (x==0)?  x+y :  x * 3 + y ;
  36.         }
  37.         ox[x] = 'X';
  38.        
  39.        
  40.         tmp = strchr(ox, '*');  //最後一步判斷
  41.        
  42.         index = (int)(tmp-ox);
  43.        
  44.         if (index < 0){
  45.             OOXX(ox);
  46.             checkWinner(ox);
  47.             return 0;
  48.         }
  49.        
  50.        
  51.         while ( ox[com] == 'X' || ox[com] == 'O') //電腦
  52.             com = (rand() % 9);
  53.         ox[com] ='O';
  54.        
  55.         checkWinner(ox);
  56.     }
  57.     return 0;
  58. }
  59.  
  60. void OOXX(char * ox){
  61.     int i;
  62.     for (i=0; i<=8; i++) { //輸出井字
  63.         printf("%c",ox[i]);
  64.         if (i % 3 ==2 )
  65.             printf("\n");
  66.     }
  67. }
  68. void checkWinner(char * ox){
  69.     int check;
  70.     for (check=0; check<=2; check++) {  //判斷
  71.        
  72.         if (ox[ check*3 % 9 ] == 'X' && ox[ (check*3+1) % 9 ] == 'X' && ox[ (check*3+2) % 9 ] == 'X' )  //橫列
  73.             printf("You Win\n");
  74.         else if (ox[ check*3 % 9 ] == 'O' && ox[ (check*3+1) % 9 ] == 'O' && ox[ (check*3+2) % 9 ] == 'O' )
  75.             printf("You Lose\n");
  76.        
  77.         if (ox[ check ] == 'X' && ox[ check + 3 ] == 'X' && ox [ check+6  ] == 'X' ) //直條
  78.             printf("You Win\n");
  79.         else if (ox[ check ] == 'O' && ox[ check + 3 ] == 'O' && ox [ check+6  ] == 'O' )
  80.             printf("You Lose\n");
  81.        
  82.     }
  83.    
  84.     if ( ox[0] =='X' && ox[4] == 'X' && ox[8] == 'X' ) //兩斜邊
  85.         printf("You Win\n");
  86.     if  ( ox[0] =='O' && ox[4] == 'O' && ox[8] == 'O' )
  87.         printf("You Lose\n");
  88.     if ( ox[2] =='X' && ox[4] == 'X' && ox[6] == 'X' )
  89.         printf("You Win\n");
  90.     if  ( ox[2] =='O' && ox[4] == 'O' && ox[6] == 'O' )
  91.         printf("You Lose\n");
  92. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement