-
Notifications
You must be signed in to change notification settings - Fork 0
/
FP.c
131 lines (129 loc) · 2.89 KB
/
FP.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
#include<stdio.h>
#include<stdlib.h>
#include<time.h>
char pos[9];
int x,y,count=0,in;
void hasil();
void menu();
void mode1();
void mode2();
void init(){
for(int i=0;i<9;i++){
pos[i]='.';
}
}
void print(){
system("clear");
printf("TIC TAC TOE\n");
for(int i=0;i<9;i++){
printf("%c ",pos[i]);
if(i==2||i==5||i==8){
printf("\n");
}
}
}
void cek(){
if(pos[(x-1)*3+y-1]=='.'){
switch(count%2){
case 0:
pos[(x-1)*3+y-1]='X';
break;
case 1:
pos[(x-1)*3+y-1]='O';
break;
}
hasil();
}else{
switch(in){
case 1:mode1();break;
default:break;
}
}
}
void hasil(){
char var;
if(count%2==0){
var='X';
}else{
var='O';
}
if((pos[4]==var&&pos[1]==var&&pos[7]==var)||
(pos[4]==var&&pos[3]==var&&pos[5]==var)||
(pos[4]==var&&pos[0]==var&&pos[8]==var)||
(pos[4]==var&&pos[2]==var&&pos[6]==var)||
(pos[0]==var&&pos[1]==var&&pos[2]==var)||
(pos[0]==var&&pos[3]==var&&pos[6]==var)||
(pos[6]==var&&pos[7]==var&&pos[8]==var)||
(pos[8]==var&&pos[5]==var&&pos[2]==var)){
if(count%2==0){
print();
printf("PLAYER 1 WIN\n");
}else{
print();
printf("PLAYER 2 WIN\n");
}
}else{
if(count<8){
count++;
switch(in){
case 1:mode1();break;
case 2:mode2();break;
default:break;
}
}else{
print();
printf("DRAW\n");
}
}
printf("PLAY A NEW GAME !!!\n<press 1 to continue>\n");
scanf("%d", &in);
if(in==1){
system("clear");
count=0;
init();
menu();
}
}
void mode1(){
print();
(count%2==0)?printf("Player 1:\ninput [baris kolom] : "):printf("Player 2:\ninput [baris kolom] : ");
scanf("%d %d", &x, &y);
cek();
}
void mode2(){
int comp;
int temp=0;
print();
if(count%2==0){
printf("Player:\ninput [baris kolom] : ");
scanf("%d %d", &x, &y);
cek();
}else{
srand(time(0));
comp=rand()%(9-count);
for(int i=0;i<9;i++){
if(pos[i]=='.'){
temp++;
if(temp==comp){
x=i/3+1;
y=i%3+1;
cek();
}
}
}
}
}
void menu(){
printf("TIC TAC TOE\n");
printf("Menu :\n1. Player vs. Player\n2. Player vs. Ez_Bot\n3. Player vs. Hell_Bot\nInput : ");
scanf("%d", &in);
if(in==1){
mode1();
}else if(in==2){
mode2();
}
}
int main(){
init();
menu();
}