-
Notifications
You must be signed in to change notification settings - Fork 0
/
Connect4.c
654 lines (591 loc) · 25.7 KB
/
Connect4.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
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
// SpaceInvaders.c
// Runs on LM4F120/TM4C123
// Jonathan Valvano and Daniel Valvano
// This is a starter project for the edX Lab 15
// In order for other students to play your game
// 1) You must leave the hardware configuration as defined
// 2) You must not add/remove any files from the project
// 3) You must add your code only this this C file
// I.e., if you wish to use code from sprite.c or sound.c, move that code in this file
// 4) It must compile with the 32k limit of the free Keil
// April 10, 2014
// http://www.spaceinvaders.de/
// sounds at http://www.classicgaming.cc/classics/spaceinvaders/sounds.php
// http://www.classicgaming.cc/classics/spaceinvaders/playguide.php
/* This example accompanies the books
"Embedded Systems: Real Time Interfacing to Arm Cortex M Microcontrollers",
ISBN: 978-1463590154, Jonathan Valvano, copyright (c) 2013
"Embedded Systems: Introduction to Arm Cortex M Microcontrollers",
ISBN: 978-1469998749, Jonathan Valvano, copyright (c) 2013
Copyright 2014 by Jonathan W. Valvano, [email protected]
You may use, edit, run or distribute this file
as long as the above copyright notice remains
THIS SOFTWARE IS PROVIDED "AS IS". NO WARRANTIES, WHETHER EXPRESS, IMPLIED
OR STATUTORY, INCLUDING, BUT NOT LIMITED TO, IMPLIED WARRANTIES OF
MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE APPLY TO THIS SOFTWARE.
VALVANO SHALL NOT, IN ANY CIRCUMSTANCES, BE LIABLE FOR SPECIAL, INCIDENTAL,
OR CONSEQUENTIAL DAMAGES, FOR ANY REASON WHATSOEVER.
For more information about my classes, my research, and my books, see
http://users.ece.utexas.edu/~valvano/
*/
// ******* Required Hardware I/O connections*******************
// Slide pot pin 1 connected to ground
// Slide pot pin 2 connected to PE2/AIN1
// Slide pot pin 3 connected to +3.3V
// fire button connected to PE0
// special weapon fire button connected to PE1
// 8*R resistor DAC bit 0 on PB0 (least significant bit)
// 4*R resistor DAC bit 1 on PB1
// 2*R resistor DAC bit 2 on PB2
// 1*R resistor DAC bit 3 on PB3 (most significant bit)
// LED on PB4
// LED on PB5
// Blue Nokia 5110
// ---------------
// Signal (Nokia 5110) LaunchPad pin
// Reset (RST, pin 1) connected to PA7
// SSI0Fss (CE, pin 2) connected to PA3
// Data/Command (DC, pin 3) connected to PA6
// SSI0Tx (Din, pin 4) connected to PA5
// SSI0Clk (Clk, pin 5) connected to PA2
// 3.3V (Vcc, pin 6) power
// back light (BL, pin 7) not connected, consists of 4 white LEDs which draw ~80mA total
// Ground (Gnd, pin 8) ground
// Red SparkFun Nokia 5110 (LCD-10168)
// -----------------------------------
// Signal (Nokia 5110) LaunchPad pin
// 3.3V (VCC, pin 1) power
// Ground (GND, pin 2) ground
// SSI0Fss (SCE, pin 3) connected to PA3
// Reset (RST, pin 4) connected to PA7
// Data/Command (D/C, pin 5) connected to PA6
// SSI0Tx (DN, pin 6) connected to PA5
// SSI0Clk (SCLK, pin 7) connected to PA2
// back light (LED, pin 8) not connected, consists of 4 white LEDs which draw ~80mA total
#include "tm4c123gh6pm.h"
#include "Nokia5110.h"
#include "Random.h"
#include "TExaS.h"
#include <string.h>
#include <stdlib.h>
#include <math.h>
// *************************** Images ***************************
// width=16 x height=10
#define BOARD_ROWS 6
#define BOARD_COLS 7
const unsigned char logo1[] ={
0x42, 0x4D, 0x3E, 0x06, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x3E, 0x00, 0x00, 0x00, 0x28, 0x00, 0x00, 0x00, 0x80, 0x00, 0x00, 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FF, 0xFF, 0xFF, 0xFF,
0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
};
void printBoard(char *board);
int takeTurn(char *board, int player, const char*);
int takeTurnRemote(char *board, int player, const char*);
int takeTurnAI(char *board, int player, const char* , int i);
int hasEmptyCol(char *board, int col );
int changeBoard(char *board,int player , const char *PIECES , int col );
int checkWin(char *board);
int checkFour(char *board, int, int, int, int);
int horizontalCheck(char *board);
int verticalCheck(char *board);
int diagonalCheck(char *board);
void Delay100ms(unsigned long count); // time delay in 0.1 seconds
#define GPIO_PORTF_DATA_R (*((volatile unsigned long *)0x400253FC))
#define GPIO_PORTF_DIR_R (*((volatile unsigned long *)0x40025400))
#define GPIO_PORTF_AFSEL_R (*((volatile unsigned long *)0x40025420))
#define GPIO_PORTF_PUR_R (*((volatile unsigned long *)0x40025510))
#define GPIO_PORTF_DEN_R (*((volatile unsigned long *)0x4002551C))
#define GPIO_PORTF_LOCK_R (*((volatile unsigned long *)0x40025520))
#define GPIO_PORTF_CR_R (*((volatile unsigned long *)0x40025524))
#define GPIO_PORTF_AMSEL_R (*((volatile unsigned long *)0x40025528))
#define GPIO_PORTF_PCTL_R (*((volatile unsigned long *)0x4002552C))
#define SYSCTL_RCGC2_R (*((volatile unsigned long *)0x400FE108))
#define SYSCTL_RCGC2_GPIOF 0x00000020 // port F Clock Gating Control
unsigned long SW1,SW2; // input from PF4,PF0
unsigned long Out; // outputs to PF3,PF2,PF1 (multicolor LED)
void PortF_Init(void);
void UARTB_init(void);
char UARTB_InChar(void) ;
void UARTB_OutChar( char data);
void UARTB_outString(char* buffer) ;
int selectMode(void);
void startingScreen(void);
int r = 0 , rr = 0 , uturn = 0 , f = 0 , i = 0 , j = 0 ;
int turn=0, done = 0;
const char *PIECES = "XO";
char board1[(BOARD_ROWS * BOARD_COLS)+1];
unsigned int seed = 25 ;
int last = 0 , lastOne = -1 ;
void PortF_Init(void){ volatile unsigned long delay;
SYSCTL_RCGC2_R |= 0x00000020| 0x00000002; // 1) B & F clock
delay = SYSCTL_RCGC2_R; // delay
GPIO_PORTF_LOCK_R = 0x4C4F434B; // 2) unlock PortF PF0
GPIO_PORTF_CR_R = 0x1F; // allow changes to PF4-0
GPIO_PORTF_AMSEL_R = 0x00; // 3) disable analog function
GPIO_PORTF_PCTL_R = 0x00000000; // 4) GPIO clear bit PCTL
GPIO_PORTF_DIR_R = 0x0E; // 5) PF4,PF0 input, PF3,PF2,PF1 output
GPIO_PORTF_AFSEL_R = 0x00; // 6) no alternate function
GPIO_PORTF_PUR_R = 0x11; // enable pullup resistors on PF4,PF0
GPIO_PORTF_DEN_R = 0x1F; // 7) enable digital pins PF4-PF0
}
int main(void){
//TExaS_Init(SSI0_Real_Nokia5110_Scope); // set system clock to 80 MHz
// initialization goes here
int mode ;
PortF_Init(); // Call initialization of port PF4, PF3, PF2, PF1, PF0
for(i = 0 ; i < BOARD_COLS*BOARD_ROWS +1;i++)
{
board1[i] = ' ';
//input[i]=' ';
}
Nokia5110_Init();
UARTB_init();
Nokia5110_Clear();
startingScreen();
mode = selectMode();
srand(seed);
Nokia5110_Clear();
if(mode)
{
r = (rand()%9)+'0';
UARTB_OutChar(r);
rr = UARTB_InChar();
if(r<rr)
{
uturn = 1 ;
}
printBoard(board1);
for(turn = 0; turn < (BOARD_ROWS * BOARD_COLS) && !done; turn++){ // 42
if(uturn)
{
do{
printBoard(board1);
}
while(!takeTurnRemote(board1, (turn) % 2, PIECES));
done = checkWin(board1);
turn++;
if(done) break;
do{
printBoard(board1);
}
while(!takeTurn(board1, (turn) % 2, PIECES));
done = checkWin(board1);
}
else
{
do{
printBoard(board1);
}
while(!takeTurn(board1, (turn) % 2, PIECES));
done = checkWin(board1);
turn++;
if(done) break;
do{
printBoard(board1);
}
while(!takeTurnRemote(board1, (turn) % 2, PIECES));
done = checkWin(board1);
}
}
printBoard(board1);
if(turn == BOARD_ROWS * BOARD_COLS && !done){
Nokia5110_OutString("It's a tie!");
} else {
turn--;
Nokia5110_Clear();
Nokia5110_SetCursor(1,1);
Nokia5110_OutString("Player");
Nokia5110_SetCursor(8,1);
Nokia5110_OutString(turn%2==0?"X":"O");
Nokia5110_SetCursor(3,2);
Nokia5110_OutString("wins!");
Nokia5110_SetCursor(1,4);
Nokia5110_OutString("GAME OVER");
}
}
else
{
printBoard(board1);
for(turn = 0; turn < (BOARD_ROWS * BOARD_COLS) && !done; turn++){ // 42
do{
printBoard(board1);
}
while(!takeTurnAI(board1, (turn) % 2, PIECES , turn));
done = checkWin(board1);
turn++;
if(done) break;
do{
printBoard(board1);
}
while(!takeTurn(board1, (turn) % 2, PIECES));
done = checkWin(board1);
}
printBoard(board1);
if(turn == BOARD_ROWS * BOARD_COLS && !done){
Nokia5110_OutString("It's a tie!");
} else {
turn--;
Nokia5110_Clear();
Nokia5110_SetCursor(1,1);
Nokia5110_OutString("Player");
Nokia5110_SetCursor(8,1);
Nokia5110_OutString(turn%2==0?"X":"O");
Nokia5110_SetCursor(3,2);
Nokia5110_OutString("wins!");
Nokia5110_SetCursor(1,4);
Nokia5110_OutString("GAME OVER");
}
}
}
void startingScreen()
{
int w;
Nokia5110_SetCursor(0,0);
Nokia5110_ClearBuffer();
//Nokia5110_PrintBMP(10, 35, logo1, 2);
//Nokia5110_DisplayBuffer();
Nokia5110_SetCursor(2,2);
Nokia5110_OutString("Connect4");
Nokia5110_SetCursor(0,5);
Nokia5110_OutString("Loading ...");
Delay100ms(40);
for(w=7 ; w>0 ; w--)
{
Nokia5110_SetCursor(0,w);
Nokia5110_OutString(" ");
Delay100ms(5);
}
}
int selectMode(){ // here is selecting if the mode is P1 VS AI or PI vs P2
int k = 0 ;
Nokia5110_SetCursor(0,0);
Nokia5110_OutString("choose");
Nokia5110_SetCursor(7,0);
Nokia5110_OutString("mode");
Nokia5110_SetCursor(3,2);
Nokia5110_OutString("P1 VS AI");
Nokia5110_SetCursor(3,4);
Nokia5110_OutString("P1 VS P2");
Nokia5110_SetCursor(0,k+2);
Nokia5110_OutString(">>");
while(1)
{
SW1 = GPIO_PORTF_DATA_R&0x10; // read PF4 into SW1
if(!SW1)
{
while (!(GPIO_PORTF_DATA_R&0x10)){
Delay100ms(1);
}
Nokia5110_SetCursor(0,k+2);
Nokia5110_OutString(" ");
k+=2;
if(k>2)
k=0;
Nokia5110_SetCursor(0,k+2);
Nokia5110_OutString(">>");
}
SW2 = GPIO_PORTF_DATA_R&0x01; // read PF4 into SW2
Delay100ms(1);
if(!SW2)
{
while (!(GPIO_PORTF_DATA_R&0x01))
{
Delay100ms(1);
seed++;
}
Nokia5110_SetCursor(3,k+2);
Nokia5110_OutString(" ");
Delay100ms(5);
Nokia5110_SetCursor(3,k+2);
Nokia5110_OutString(k?"P1 VS P2":"P1 VS AI");
Delay100ms(5);
Nokia5110_SetCursor(3,k+2);
Nokia5110_OutString(" ");
Delay100ms(5);
Nokia5110_SetCursor(3,k+2);
Nokia5110_OutString(k?"P1 VS P2":"P1 VS AI");
break ;
}
seed++;
}
return k ;
}
void printBoard(char *board){
int row, col;
Nokia5110_ClearBuffer();
for(row = 0; row < BOARD_ROWS; row++){
for(col = 0; col < BOARD_COLS; col++){
Nokia5110_SetCursorChar( col ,row,board[BOARD_COLS*row+col]);
}
}
}
// take turn for the Kit player and PIECES can be X or O
int takeTurn(char *board, int player, const char *PIECES){
int col = 0;
int g ;
while(1){
SW1 = GPIO_PORTF_DATA_R&0x10; // read PF4 into SW1
Nokia5110_SetCursorChar( col ,0,PIECES[player]);
if(!SW1)
{
while (!(GPIO_PORTF_DATA_R&0x10)){
Delay100ms(1);
}
col++;
if(col>6)
col = 0 ;
Nokia5110_SetCursorChar( col ,0,PIECES[player]);
g = col==0?6:col-1;
Nokia5110_SetCursorChar( g,0,board[g]);
}
SW2 = GPIO_PORTF_DATA_R&0x01; // read PF4 into SW2
Delay100ms(1);
if(!SW2)
{
while (!(GPIO_PORTF_DATA_R&0x01))
{
Delay100ms(1);
}
break ;
}
}
UARTB_OutChar(col+1+'0');
last = col ;
return changeBoard(board , player , PIECES , col );
}
//For AI
int takeTurnAI(char *board, int player, const char*PIECES , int i )
{
int col ; //= rand()%7;
if(i==0 || hasEmptyCol(board,3) )
{
col = 3 ;
}
else{
if(hasEmptyCol(board,lastOne))
{
col = lastOne;
}
else if(hasEmptyCol(board,last))
{
col = last;
}
else {
if(last >0 && last < 6)
{
if(hasEmptyCol(board,last-1))
col = last-1 ;
else if(hasEmptyCol(board,last+1))
col = last+1;
else if(hasEmptyCol(board,lastOne)&&lastOne>-1)
col = lastOne;
}
}
}
lastOne = col ;
return changeBoard(board , player , PIECES , col );
}
//For UART move
int takeTurnRemote(char *board, int player, const char *PIECES){
int col = 0;
col = UARTB_InChar();
col=col-1-'0' ;
if(col>6)
{
col = 0 ;
}
return changeBoard(board , player , PIECES , col );
}
int changeBoard(char *board,int player , const char *PIECES , int col )
{
int row ;
for(row = BOARD_ROWS - 1; row >= 0; row--){
if(board[BOARD_COLS * row + col] == ' '){
board[BOARD_COLS * row + col] = PIECES[player];
return 1;
}
}
return 0 ;
}
int hasEmptyCol(char *board, int col )
{
int row ;
for(row = BOARD_ROWS - 1; row >= 0; row--){
if(board[BOARD_COLS * row + col] == ' '){
return 1;
}
}
return 0 ;
}
int checkWin(char *board){
return (horizontalCheck(board) || verticalCheck(board) || diagonalCheck(board));
}
int checkFour(char *board, int a, int b, int c, int d){
return (board[a] == board[b] && board[b] == board[c] && board[c] == board[d] && board[a] != ' ');
}
int horizontalCheck(char *board){
int row, col, idx;
const int WIDTH = 1;
for(row = 0; row < BOARD_ROWS; row++){
for(col = 0; col < BOARD_COLS - 3; col++){
idx = BOARD_COLS * row + col;
if(checkFour(board, idx, idx + WIDTH, idx + WIDTH * 2, idx + WIDTH * 3)){
return 1;
}
}
}
return 0;
}
int verticalCheck(char *board){
int row, col, idx;
const int HEIGHT = 7;
for(row = 0; row < BOARD_ROWS - 3; row++){
for(col = 0; col < BOARD_COLS; col++){
idx = BOARD_COLS * row + col;
if(checkFour(board, idx, idx + HEIGHT, idx + HEIGHT * 2, idx + HEIGHT * 3)){
return 1;
}
}
}
return 0;
}
int diagonalCheck(char *board){
int row, col, idx, count = 0;
const int DIAG_RGT = 6, DIAG_LFT = 8;
for(row = 0; row < BOARD_ROWS - 3; row++){
for(col = 0; col < BOARD_COLS; col++){
idx = BOARD_COLS * row + col;
if((count <= 3 && checkFour(board, idx, idx + DIAG_LFT, idx + DIAG_LFT * 2, idx + DIAG_LFT * 3)) || (count >= 3 && checkFour(board, idx, idx + DIAG_RGT, idx + DIAG_RGT * 2, idx + DIAG_RGT * 3))){
return 1;
}
count++;
}
count = 0;
}
return 0;
}
//
void UARTB_init()
{
SYSCTL_RCGCUART_R |= (1<<1);
GPIO_PORTB_AFSEL_R |= 0x03;
GPIO_PORTB_DEN_R |= 0x03;
GPIO_PORTB_PCTL_R |= 0x11;
UART1_CTL_R &= ~(1<<0) ;
UART1_IBRD_R = 8 ;
UART1_FBRD_R = 44 ;
// IBRD = int(16,000,000 / (16 * 115200)) = int(8.6805)
// FBRD = int(0.6805 * 64 + 0.5) = 44
UART1_LCRH_R = (3<<5);
UART1_CC_R = 0 ;
UART1_CTL_R |= (1<<0) | (1<<8)| (1<<9);
}
char UARTB_InChar(void){
// as part of Lab 11, modify this program to use UART0 instead of UART1
while((UART1_FR_R&UART_FR_RXFE) != 0);
return(( char)(UART1_DR_R));
}
void UARTB_OutChar( char data){
// as part of Lab 11, modify this program to use UART0 instead of UART1
while((UART1_FR_R&(1<<5)) != 0);
UART1_DR_R = data;
}
void UARTB_outString(char* buffer)
{
while(*buffer != 0 )
{
UARTB_OutChar(*buffer) ;
buffer++;
}
}
/*
void UARTB_init()
{
SYSCTL_RCGCUART_R |= (1<<0);
GPIO_PORTA_AFSEL_R |= 0x03;
GPIO_PORTA_DEN_R |= 0x03;
GPIO_PORTA_PCTL_R |= 0x11;
UART0_CTL_R &= ~(1<<0) ;
UART0_IBRD_R = 104 ;
UART0_FBRD_R = 11;
// IBRD = int(80,000,000 / (16 * 9600)) = int(43.4027)
// FBRD = int(0.4027 * 64 + 0.5) = 26
UART0_LCRH_R = (3<<5);
UART0_CC_R = 0 ;
UART0_CTL_R |= (1<<0) | (1<<8)| (1<<9);
}
char UARTB_InChar(void){
// as part of Lab 11, modify this program to use UART0 instead of UART1
while((UART0_FR_R&UART_FR_RXFE) != 0);
return(( char)(UART0_DR_R));
}
void UARTB_OutChar( char data){
// as part of Lab 11, modify this program to use UART0 instead of UART1
while((UART0_FR_R&(1<<5)) != 0);
UART0_DR_R = data;
}
void UARTB_outString(char* buffer)
{
while(*buffer != 0 )
{
UARTB_OutChar(*buffer) ;
buffer++;
}
}
//*/
void Delay100ms(unsigned long count){unsigned long volatile time;
while(count){
time = 72724; // 0.1sec at 80 MHz
while(time){
time--;
}
count--;
}
}