forked from badrobit/Connect6
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathConnect6.c
More file actions
148 lines (123 loc) · 2.51 KB
/
Connect6.c
File metadata and controls
148 lines (123 loc) · 2.51 KB
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
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
#define BOARD_SIZE 19
#include <stdio.h>
#include <stdlib.h>
void initialize_game_board();
void print_game_board();
void human_place_piece();
void ai_move_offensively();
void play_move( int x, int y );
char game_board[BOARD_SIZE][BOARD_SIZE];
int start_move_x = 5;
int start_move_y = 5;
int algorithm_progress = 0;
int main( void )
{
initialize_game_board();
while( 1 )
{
human_place_piece();
ai_move_offensively();
}
return(0);
}
// A function which allows a user to place a piece on the
// game board.
void human_place_piece()
{
int x, y = 0;
printf( "Enter Coordonate to Place Piece In\n" );
scanf( "%d", &x );
scanf( "%d", &y );
game_board[x-1][y-1] = 'B';
printf( "Enter Coordonate to Place Piece In\n" );
scanf( "%d", &x );
scanf( "%d", &y );
game_board[x-1][y-1] = 'B';
print_game_board();
}
// A function that will capture the offensive (game winning)
// movemnets of the AI.
void ai_move_offensively()
{
int t = 0;
int x = start_move_x;
int y = start_move_y;
int planning = 1;
if( algorithm_progress == 0 )
{
play_move( x, y );
play_move( x, y+1 );
algorithm_progress += 1;
}
else if( algorithm_progress == 1 )
{
play_move( x, y+2 );
play_move( x, y+3 );
algorithm_progress += 1;
}
else if( algorithm_progress == 2 )
{
play_move( x+1, y+3 );
play_move( x-1, y+3 );
algorithm_progress += 1;
}
else if( algorithm_progress == 3 )
{
play_move( x+2, y );
play_move( x+1, y+1 );
algorithm_progress += 1;
}
else if( algorithm_progress == 4 )
{
play_move( x+1, y );
play_move( x+1, y+2 );
algorithm_progress += 1;
}
else if( algorithm_progress == 5 )
{
play_move( x-1, y );
play_move( x-2, y-1 );
algorithm_progress += 1;
}
print_game_board();
}
// Used to print out the game board for visual display to the
// user.
void print_game_board()
{
int x, y = 0;
for( x = 0; x < (BOARD_SIZE*2)+1; x++ )
{
if( x%2 == 0 )
printf( "+" );
if( x%2 != 0 )
printf( "|" );
for( y = 0; y < BOARD_SIZE; y++ )
{
if( x%2 == 0 )
printf( "---+" );
if ( x%2 != 0 )
printf( " %c |", game_board[x/2][y] );
}
printf( "\n" );
}
}
void initialize_game_board()
{
int x, y = 0;
for( x = 0; x < BOARD_SIZE; x++ )
{
for( y = 0; y < BOARD_SIZE; y++ )
{
game_board[x][y] = ' ';
}
}
}
void play_move( int x, int y )
{
if( x > 1 && x < BOARD_SIZE && y > 1 && y < BOARD_SIZE && game_board[x-1][y-1] == ' ' )
{
printf( "Play Move: ( %d, %d )\n", x, y );
game_board[x-1][y-1] = 'W';
}
}