-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCprogram
More file actions
1941 lines (1626 loc) · 71.4 KB
/
Cprogram
File metadata and controls
1941 lines (1626 loc) · 71.4 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
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
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
# lab0
/*
* File: main.c
* Author: yangjiaw
*
* Created on September 13, 2015, 6:49 PM
*/
#include <stdio.h>
#include <stdlib.h>
/*
*
*/
int main(int argc, char** argv)
{
printf("This is my Lab 0 program!\n");
return (EXIT_SUCCESS);
}
# lab 1
/*
* File: main.c
* Author: yangjiaw
*
* Created on September 13, 2015, 7:40 PM
*/
#include <stdio.h>
#include <stdlib.h>
/*
*
*/
int main(int argc, char** argv)
{
int inputNumber1,inputNumber2;//reserve two positions for two number
printf("Enter First Number: ");
scanf("%d",&inputNumber1);
printf("Enter Second Number: ");
scanf("%d",&inputNumber2);// receive two numbers
int sum = inputNumber1+inputNumber2,difference = inputNumber1-inputNumber2;
int squareofNumber1= inputNumber1*inputNumber1, squareofNumber2= inputNumber2*inputNumber2;
int division = inputNumber2/inputNumber1;//identify each value and do the calculation
printf("Sum: %d\nDifference: %d\nSquare of number1: %d\nSquare of number2: %d\nDivision: %d\n",sum,difference,squareofNumber1,squareofNumber2,division);
return (EXIT_SUCCESS);
}
# lab 2 part 1
/*
* File: main.c
* Author: yangjiaw
*
* Created on September 21, 2015, 1:21 PM
*/
#include <stdio.h>
#include <stdlib.h>
/*
*
*/
int main(int argc, char** argv)
{
const double A=1.5;
const double B=10;
const double C=20.0/125;
double patientAge,doctorsEstimate,Likelihood;
int BNTP;//identify all the variables
printf("Enter patient age: ");
scanf("%lf",&patientAge);
printf("Enter doctor %% likelihood guess of heart failure: ");
scanf("%lf",&doctorsEstimate);
printf("Enter patient BNPT value (between 0 and 1000 pg/ml): ");
scanf("%d",&BNTP);//tell the user to type the value and receive them
Likelihood=(doctorsEstimate/A)+(patientAge/B)+(BNTP*C);//do the calculation
printf("Computed likelihood of heart failure is %.1lf%%",Likelihood);//print the result
return (EXIT_SUCCESS);
}
#lab 2 part 2
/*
* File: main.c
* Author: yangjiaw
*
* Created on September 21, 2015, 1:51 PM
*/
#include <stdio.h>
#include <stdlib.h>
/*
*
*/
int main(int argc, char** argv) {
int B0,B1,B2,B3,input,answer;
printf("Enter 4-bit binary number:");
scanf("%d",&input);
B3=input/1000;//put all numbers in the right positions
B2=(input-1000*B3)/100;
B1=(input-1000*B3-100*B2)/10;
B0=(input-1000*B3-100*B2-10*B1)/1;
answer=(B3*8)+(B2*4)+(B1*2)+B0;//calculate the final answer
printf("%d in base 2 is %d in base 10",input,answer);
return (EXIT_SUCCESS);
}
# lab 3 part 1
/*
* File: main.c
* Author: yangjiaw
*
* Created on October 8, 2015, 10:37 AM
*/
#include <stdio.h>
#include <stdlib.h>
/*
*
*/
int main(int argc, char** argv) {
double firstNumber,secondNumber;
char inChar;
printf("Enter first number: ");
scanf("%lf",&firstNumber);
printf("Enter second number: ");
scanf("%lf",&secondNumber);
printf("Enter calculation command (one of a, s, m, or d): ");
scanf(" %c",&inChar);
if (secondNumber==0 && inChar=='d')
printf("Error, trying to divide by zero");
else
{
switch (inChar)
{
case 'a':printf("Sum of %.2lf and %.2lf is %.2lf",firstNumber,secondNumber,firstNumber+secondNumber);
break;
case 's':printf("Difference of %.2lf from %.2lf is %.2lf",firstNumber,secondNumber,firstNumber-secondNumber);
break;
case 'm':printf("Product of %.2lf and %.2lf is %.2lf",firstNumber,secondNumber,firstNumber*secondNumber);
break;
case 'd':printf("Division of %.2lf by %.2lf is %.2lf",firstNumber,secondNumber,firstNumber/secondNumber);
break;
default :printf("Error, unknown calculation command given\n");
break;}
}
return (EXIT_SUCCESS);
}
# lab 3 part 2
/*
* File: main.c
* Author: yangjiaw
*
* Created on October 7, 2015, 1:03 PM
*/
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
/*
*
*/
int main(int argc, char** argv)
{
int seed;
int guessNumber=1;
int answer;
printf("Enter seed: ");
scanf("%d",&seed);
srand(seed);
while(guessNumber>0)
{
printf("Guess a number from 1 to 7 (<=0 to quit): ");
scanf("%d",&guessNumber);
if(guessNumber>7)
printf("Number too high, guess again!\n");
else if (guessNumber>0)
{
answer=rand()%7+1;
if (guessNumber == answer)
{
printf("Number picked: %d\n",answer);
printf("Good guess!\n");
}
else if(guessNumber!=answer && guessNumber<=7)
{
printf("Number picked: %d\n",answer);
printf("Sorry, try again!\n");
}
}
}printf("Goodbye");
return (EXIT_SUCCESS);
}
# lab 4
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
double gaussFunc(double x,double spread)
{
double result;
result=exp(-x*x/spread);
return result;
}
int main(void){
double spread=0.0,a,b,midpoint,result=0.0,range;
int n=2;
double gaussFunc(double x, double spread);
while(spread<=0){
printf("Enter the spread value (must be >0): ");
scanf("%lf",&spread);
}
while (n>=1){
printf("Please enter the number of rectangles (n): ");
scanf("%d",&n);
if (n<1)
break;
else {
printf("Enter the interval of integration (a b): ");
scanf("%lf %lf",&a,&b);
while (a>b){
printf("Invalid interval!\n");
printf("Enter the interval of integration (a b): ");
scanf("%lf %lf",&a,&b);
}
range=(b-a)/n;
midpoint=a+range/2;
while (midpoint<=b-range/2) {
result=result+gaussFunc(midpoint, spread)*range;
midpoint+=range;
}
printf("Integral of Gaussian with spread %.3lf on [%.3lf, %.3lf] with n = %d is: %.3lf \n",spread,a,b,n,result);
result=0;}}
printf("Exiting.");
return 0;
}
# lab 5
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
const char DNA[] = {'A', 'G', 'C', 'G', 'G', 'G', 'A', 'C', 'C', 'G', 'T', 'C',
'C', 'C', 'G', 'A', 'C', 'A', 'T', 'T', 'G', 'A', 'T', 'G',
'A', 'A', 'G', 'G', 'G', 'T', 'C', 'A', 'T', 'A', 'G', 'A',
'C', 'C', 'C', 'A', 'A', 'T', 'A', 'C', 'G', 'C', 'C', 'A',
'C', 'C', 'A', 'C', 'C', 'C', 'C', 'A', 'A', 'G', 'T', 'T',
'T', 'T', 'C', 'C', 'T', 'G', 'T', 'G', 'T', 'C', 'T', 'T',
'C', 'C', 'A', 'T', 'T', 'G', 'A', 'G', 'T', 'A', 'G', 'A',
'T', 'T', 'G', 'A', 'C', 'A', 'C', 'T', 'C', 'C', 'C', 'A',//identify the DNA sequence as a constant
'G', 'A', 'T', 'G', '\0'};
void readSequence(int length,char inputSequence[]){
int count;
printf("Enter %d characters (one of AGTC*) as a search sequence: ",length);
for (count=0;count<length;count++) {
scanf(" %c",&inputSequence[count]);
}
}//A function that is used to read letters typed and put it into a sequence
bool elementCheck(char inputSequence[],int length){
bool rightCharacter=true;
int i;
for (i=0; i<length; i++) {
if(inputSequence[i]!='A'&&inputSequence[i]!='G'&&inputSequence[i]!='T'&&inputSequence[i]!='C'&&inputSequence[i]!='*')
rightCharacter=false;
}
return rightCharacter;
}//A function that used to check whether all the element fit the requirement, then return a bool value
void searchElement(char inputSequence[],char DNA[],int length){
int correctPosition=0,i,j=0;
for (j=0;j<101;j++){
i=0;
while(((inputSequence[i]=='*')||(DNA[j+i]==inputSequence[i]))&&(i<length)&&(DNA[i+j]!='\0')){
i++;
if (i==length){
correctPosition=j;
printf("Match of search sequence found at element %d\n",correctPosition);}
}
}
}//a function that actually do the search work and return the correct position of the first letter
int main(void) {
int length = 1;
while (length>0){//a while loop to keep asking for length and value of sequence
int count=0;// if there exist wrong letter, count will help us to print it out
printf("Enter length of DNA sequence to match: ");
scanf("%d",&length);
char inputSequence[length];
if(length<=0){// judging that if length is less than or equal to zero, and jump out of the loop when it is
break;
}
else {
readSequence(length,inputSequence);
if (elementCheck(inputSequence,length)==true){
searchElement(inputSequence,DNA,length);
}
if (elementCheck(inputSequence, length)==false) {
while(count<length){
if((inputSequence[count]!='A')&&(inputSequence[count]!='G')&&(inputSequence[count]!='T')&&(inputSequence[count]!='C')&&(inputSequence[count]!='*'))
{
printf("Erroneous character input '%c' exiting\n",inputSequence[count]);
printf("Goodbye");
return 0;
}
count++;
}
length=-1;
}
}
}
printf("Goodbye");
return 0;
}
# lab 6
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
bool positionInBounds(int N, char row, char col) {//to determin that whether the picked position in the board or not
bool positionInBounds = false;
int rowNumber, colNumber;
rowNumber = row - 'a';
colNumber = col - 'a';
if ((rowNumber >= 0)&&(rowNumber < N)&&(colNumber >= 0)&&(colNumber < N))
positionInBounds = true;
return positionInBounds;
}
bool checkLegalIndirection(char board[26][26], int N, char row, char col, char colour, int deltaRow, int deltaCol) {
bool positionVacant = false;
bool directionLeagal = false;
char diffColor = 'B' + 'W' - colour; //determin the different colour of th picked cheese
int rowInt = row - 'a', colInt = col - 'a'; //rowInt and colInt mean the position in number form of the next move that player gonner take
if (board[rowInt][colInt] == 'U')
positionVacant = true; // to make sure that the next move must be vacant
if (positionVacant == true) {
for (deltaRow = -1; deltaRow <= 1; deltaRow++) {
for (deltaCol = -1; deltaCol <= 1; deltaCol++)//thoes two for loop is used to picked the direction that is going to be checked
{
if ((deltaRow != 0) || (deltaCol != 0)) {//deltaCol and deltaRow can not be 0 at the same time
int newRow = rowInt + deltaRow;
int newCol = colInt + deltaCol; //the position of the next cheese along the picked direction
if ((positionInBounds(N, (char) newRow + 'a', (char) newCol + 'a'))&&(board[newRow][newCol] != 'U'))//to make sure that this move is not outside the board and vacant
{
if (board[newRow][newCol] == diffColor) {// not the same color
while ((board[newRow][newCol] == diffColor) && positionInBounds(N, (char) newRow + 'a', (char) newCol + 'a')) {//must be the different color and inside the board
newRow = newRow + deltaRow;
newCol = newCol + deltaCol;
if ((board[newRow][newCol] == colour) && positionInBounds(N, (char) newRow + 'a', (char) newCol + 'a')) {
directionLeagal = true;
return directionLeagal;
}
}
}
}
}
}
}
}
return directionLeagal;
}
void printBoard(char board[26][26], int N) {//print out the whole board
int row, col;
char initialLetter = 'a';
char printedLetter;
printf(" ");
for (col = 0; col < N; col++) {
printedLetter = initialLetter + col;
printf("%c", printedLetter);
}
printedLetter = 0;
printf("\n");
for (row = 0; row < N; row++) {
printedLetter = initialLetter + row;
printf("%c", printedLetter);
printf(" ");
for (col = 0; col < N; col++)
printf("%c", board[row][col]);
printf("\n");
}
}
void changeBoard(char board[26][26], char colour, char row, char col) {// a simple function to change the board with the typed
int inputRow, inputCol;
inputRow = row - 'a';
inputCol = col - 'a';
board[inputRow][inputCol] = colour;
}
void initinBoard(char board[26][26], int N) {
int row, col;
for (row = 0; row < N; row++) {
for (col = 0; col < N; col++)
board[row][col] = 'U';
}
board[(N / 2) - 1][(N / 2) - 1] = 'W';
board[(N / 2) - 1][N / 2] = 'B';
board[N / 2][(N / 2) - 1] = 'B';
board[N / 2][N / 2] = 'W';
}
void printAvailableMove(char colour, int N, char board[26][26], int deltaRow, int deltaCol) {//print out all the available move
char row, col;
int rowNumber, colNumber;
printf("Available moves for %c: \n", colour);
for (rowNumber = 0; rowNumber < N; rowNumber++) {
for (colNumber = 0; colNumber < N; colNumber++) {
row = rowNumber + 'a';
col = colNumber + 'a';
if (checkLegalIndirection(board, N, row, col, colour, deltaRow, deltaCol)) {//using the checkLegalIndirecton
printf("%c%c\n", row, col);
}
}
}
}
void judgeAndFlip(char board[26][26], char colour, char row, char col, int N, int deltaRow, int deltaCol) {//after receiving a new move, tell it is a valiad move or not, and flip other tail
char diffColor = 'B' + 'W' - colour;
int rowInt = row - 'a', colInt = col - 'a';
int i, j;
if (checkLegalIndirection(board, N, row, col, colour, deltaRow, deltaCol) == true) {
printf("Valid move.");
changeBoard(board, colour, row, col);
for (deltaRow = -1; deltaRow <= 1; deltaRow++) {
for (deltaCol = -1; deltaCol <= 1; deltaCol++)//thoes two for loop is used to picked the direction that is going to be checked
{
if ((deltaRow != 0) || (deltaCol != 0)) {//deltaCol and deltaRow can not be 0 at the same time
int newRow = rowInt + deltaRow;
int newCol = colInt + deltaCol; //the position of the next cheese along the picked direction
if ((positionInBounds(N, (char) newRow + 'a', (char) newCol + 'a'))&&(board[newRow][newCol] != 'U'))//to make sure that this move is not outside the board and vacant
{
if (board[newRow][newCol] == diffColor) {
while ((board[newRow][newCol] == diffColor) && positionInBounds(N, (char) newRow + 'a', (char) newCol + 'a')) {
newRow = newRow + deltaRow;
newCol = newCol + deltaCol;
if (board[newRow][newCol] == colour) {//then you find the correct direction and position of the same colour cheese in this line
int rowFlip=rowInt,colFlip=colInt;
while((rowFlip!=newRow)||(colFlip!=newCol)){
board[rowFlip][colFlip]=colour;
rowFlip+=deltaRow;
colFlip+=deltaCol;
}
}
}
}
}
}
}
}
}
else
printf("Invalid move.");
}
void boardConfiguration(char board[26][26], char row, char col, char colour, int N) {//change the board
printf("Enter board configuration:\n");
while (colour != '!') {
scanf(" %c", &colour);
scanf(" %c", &row);
scanf(" %c", &col);
changeBoard(board, colour, row, col);
}
}
int main(int argc, char** argv) {
int N;
char row, col;
char board[26][26];
char colour = 0;
int deltaRow, deltaCol;
printf("Enter the board dimension: ");
scanf("%d", &N);
initinBoard(board, N);
printBoard(board, N);
boardConfiguration(board, row, col, colour, N);
printBoard(board, N);
colour = 'W';
printAvailableMove(colour, N, board, deltaRow, deltaCol);
colour = 'B';
printAvailableMove(colour, N, board, deltaRow, deltaCol);
printf("Enter a move: \n");
scanf(" %c", &colour);
scanf(" %c", &row);
scanf(" %c", &col);
judgeAndFlip(board,colour,row,col,N,deltaRow,deltaCol);
printf("\n");
printBoard(board, N);
return (EXIT_SUCCESS);
}
# lab 7 part 1
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
bool positionInBounds(int N, char row, char col) {//to determin that whether the picked position in the board or not
bool positionInBounds = false;
int rowNumber, colNumber;
rowNumber = row - 'a';
colNumber = col - 'a';
if ((rowNumber >= 0)&&(rowNumber < N)&&(colNumber >= 0)&&(colNumber < N))
positionInBounds = true;
return positionInBounds;
}
bool checkLegalIndirection(char board[26][26], int N, char row, char col, char colour, int deltaRow, int deltaCol) {
bool positionVacant = false;
bool directionLeagal = false;
char diffColor = 'B' + 'W' - colour; //determin the different colour of th picked cheese
int rowInt = row - 'a', colInt = col - 'a'; //rowInt and colInt mean the position in number form of the next move that player gonner take
if (board[rowInt][colInt] == 'U')
positionVacant = true; // to make sure that the next move must be vacant
if (positionVacant == true) {
for (deltaRow = -1; deltaRow <= 1; deltaRow++) {
for (deltaCol = -1; deltaCol <= 1; deltaCol++)//thoes two for loop is used to picked the direction that is going to be checked
{
if ((deltaRow != 0) || (deltaCol != 0)) {//deltaCol and deltaRow can not be 0 at the same time
int newRow = rowInt + deltaRow;
int newCol = colInt + deltaCol; //the position of the next cheese along the picked direction
if ((positionInBounds(N, (char) newRow + 'a', (char) newCol + 'a'))&&(board[newRow][newCol] != 'U'))//to make sure that this move is not outside the board and vacant
{
if (board[newRow][newCol] == diffColor) {// not the same color
while ((board[newRow][newCol] == diffColor) && positionInBounds(N, (char) newRow + 'a', (char) newCol + 'a')) {//must be the different color and inside the board
newRow = newRow + deltaRow;
newCol = newCol + deltaCol;
if ((board[newRow][newCol] == colour) && positionInBounds(N, (char) newRow + 'a', (char) newCol + 'a')) {
directionLeagal = true;
return directionLeagal;
}
}
}
}
}
}
}
}
return directionLeagal;
}
void printBoard(char board[26][26], int N) {//print out the whole board
int row, col;
char initialLetter = 'a';
char printedLetter;
printf(" ");
for (col = 0; col < N; col++) {
printedLetter = initialLetter + col;
printf("%c", printedLetter);
}
printedLetter = 0;
printf("\n");
for (row = 0; row < N; row++) {
printedLetter = initialLetter + row;
printf("%c", printedLetter);
printf(" ");
for (col = 0; col < N; col++)
printf("%c", board[row][col]);
printf("\n");
}
}
void changeBoard(char board[26][26], char colour, char row, char col) {// a simple function to change the board with the typed
int inputRow, inputCol;
inputRow = row - 'a';
inputCol = col - 'a';
board[inputRow][inputCol] = colour;
}
void initinBoard(char board[26][26], int N) {
int row, col;
for (row = 0; row < N; row++) {
for (col = 0; col < N; col++)
board[row][col] = 'U';
}
board[(N / 2) - 1][(N / 2) - 1] = 'W';
board[(N / 2) - 1][N / 2] = 'B';
board[N / 2][(N / 2) - 1] = 'B';
board[N / 2][N / 2] = 'W';
}
bool checkAvailableMove(char colour, int N, char board[26][26], int deltaRow, int deltaCol) {//print out all the available move
bool existAvailableMove = false;
char row, col;
int rowNumber, colNumber;
for (rowNumber = 0; rowNumber < N; rowNumber++) {
for (colNumber = 0; colNumber < N; colNumber++) {
row = rowNumber + 'a';
col = colNumber + 'a';
if ((board[rowNumber][colNumber] == 'U' && checkLegalIndirection(board, N, row, col, colour, deltaRow, deltaCol))) {//using the checkLegalIndirecton
existAvailableMove = true;
}
}
}
return existAvailableMove;
}
void changeAndFlip(char board[26][26], char colour, char row, char col, int N, int deltaRow, int deltaCol) {//after receiving a new move, tell it is a valiad move or not, and flip other tail
char diffColor = 'B' + 'W' - colour;
int rowInt = row - 'a', colInt = col - 'a';
int i, j;
changeBoard(board, colour, row, col);
for (deltaRow = -1; deltaRow <= 1; deltaRow++) {
for (deltaCol = -1; deltaCol <= 1; deltaCol++)//thoes two for loop is used to picked the direction that is going to be checked
{
if ((deltaRow != 0) || (deltaCol != 0)) {//deltaCol and deltaRow can not be 0 at the same time
int newRow = rowInt + deltaRow;
int newCol = colInt + deltaCol; //the position of the next cheese along the picked direction
if ((positionInBounds(N, (char) newRow + 'a', (char) newCol + 'a'))&&(board[newRow][newCol] != 'U'))//to make sure that this move is not outside the board and vacant
{
if (board[newRow][newCol] == diffColor) {
while ((board[newRow][newCol] == diffColor) && positionInBounds(N, (char) newRow + 'a', (char) newCol + 'a')) {
newRow = newRow + deltaRow;
newCol = newCol + deltaCol;
if ((board[newRow][newCol] == colour) && (positionInBounds(N,(char) newRow+'a', (char) newCol+'a'))) {//then you find the correct direction and position of the same colour cheese in this line+
int rowFlip = rowInt, colFlip = colInt;
while ((rowFlip != newRow) || (colFlip != newCol)) {
board[rowFlip][colFlip] = colour;
rowFlip += deltaRow;
colFlip += deltaCol;
}
}
}
}
}
}
}
}
}
int countFlip(char board[26][26], char colour, char row, char col, int N, int deltaRow, int deltaCol) {//after receiving a new move, tell it is a valiad move or not, and flip other tail
char diffColor = 'B' + 'W' - colour;
int count = 0;
int rowInt = row - 'a', colInt = col - 'a';
int i, j;
for (deltaRow = -1; deltaRow <= 1; deltaRow++) {
for (deltaCol = -1; deltaCol <= 1; deltaCol++)//thoes two for loop is used to picked the direction that is going to be checked
{
if ((deltaRow != 0) || (deltaCol != 0)) {//deltaCol and deltaRow can not be 0 at the same time
int newRow = rowInt + deltaRow;
int newCol = colInt + deltaCol; //the position of the next cheese along the picked direction
if ((positionInBounds(N, (char) newRow + 'a', (char) newCol + 'a'))&&(board[newRow][newCol] != 'U'))//to make sure that this move is not outside the board and vacant
{
if (board[newRow][newCol] == diffColor) {
while ((board[newRow][newCol] == diffColor) && positionInBounds(N, (char) newRow + 'a', (char) newCol + 'a')) {
newRow = newRow + deltaRow;
newCol = newCol + deltaCol;
if (board[newRow][newCol] == colour) {//then you find the correct direction and position of the same colour cheese in this line
int rowFlip = rowInt, colFlip = colInt;
while ((rowFlip != newRow) || (colFlip != newCol)) {
count++;
rowFlip += deltaRow;
colFlip += deltaCol;
}
count=count-1;
}
}
}
}
}
}
}
return count;
}
void boardConfiguration(char board[26][26], char row, char col, char colour, int N) {//change the board
printf("Enter board configuration:\n");
while (colour != '!') {
scanf(" %c", &colour);
scanf(" %c", &row);
scanf(" %c", &col);
changeBoard(board, colour, row, col);
}
}
void comMove(char board[26][26], int N, char comColour, int deltaRow, int deltaCol) {
char row, col, bestRow = 0, bestCol = 0, scoreBoard[26][26];
int score = 0, rowNumber, colNumber;
for (rowNumber = 0; rowNumber < N; rowNumber++)//Initial the scoreboard with all the value equal to zero
for (colNumber = 0; colNumber < N; colNumber++) {
scoreBoard[rowNumber][colNumber] = 0;
}
for (rowNumber = 0; rowNumber < N; rowNumber++) {
for (colNumber = 0; colNumber < N; colNumber++) {
row = rowNumber + 'a';
col = colNumber + 'a';
if ((board[rowNumber][colNumber]=='U')&&checkLegalIndirection(board, N, row, col, comColour, deltaRow, deltaCol))
scoreBoard[rowNumber][colNumber] = countFlip(board, comColour, row, col, N, deltaRow, deltaCol);
}
}
for (rowNumber = 0; rowNumber < N; rowNumber++) {
for (colNumber = 0; colNumber < N; colNumber++) {
if ((scoreBoard[rowNumber][colNumber] > score)//get the highest score
|| ((scoreBoard[rowNumber][colNumber] == score)&&(rowNumber < bestRow - 'a'))//when the score is the same, the best position should be the one that has lower row
|| ((scoreBoard[rowNumber][colNumber] == score)&&(rowNumber == bestRow - 'a')&&(colNumber < bestCol - 'a'))) {
score = scoreBoard[rowNumber][colNumber];
bestRow = rowNumber + 'a';
bestCol = colNumber + 'a';
}
}
}
printf("Computer places %c at %c%c.\n", comColour, bestRow, bestCol);
changeAndFlip(board, comColour, bestRow, bestCol, N, deltaRow, deltaCol);
}
void determineWiner(char board[26][26], int N) {//when the game end,determine which one is the winner
int i, j, blackCount = 0, whiteCount = 0;
for (i = 0; i < N; i++)
for (j = 0; j < N; j++) {
if (board[i][j] == 'B')
blackCount++;
if (board[i][j] == 'W')
whiteCount++;
}
if (blackCount > whiteCount)
printf("B player wins.");
if (blackCount == whiteCount)
printf("Draw!");
if (blackCount < whiteCount)
printf("W player wins.");
}
int main(int argc, char** argv) {
int N = 0;
char playerRow, playerCol, comRow, comCol;
char board[26][26];
char colour;
char comColour;
char playerColour;
int deltaRow, deltaCol;
bool validMove;
printf("Enter the board dimension: ");
scanf("%d", &N);
printf("Computer plays (B/W) : ");
scanf(" %c", &comColour);
playerColour = 'B' + 'W' - comColour;
initinBoard(board, N);
printBoard(board, N);
validMove = true;
if (playerColour == 'B') {//player need to play first once it get the B
printf("Enter move for colour %c (RowCol): ", playerColour);
scanf(" %c", &playerRow);
scanf(" %c", &playerCol);
if ((positionInBounds(N, playerRow, playerCol)
&& checkLegalIndirection(board, N, playerRow, playerCol, playerColour, deltaRow, deltaCol)
&& validMove) == true) {//always check move legal or not
changeAndFlip(board, playerColour, playerRow, playerCol, N, deltaRow, deltaCol);
printBoard(board, N);
} else if ((positionInBounds(N, playerRow, playerCol) &&
checkLegalIndirection(board, N, playerRow, playerCol, playerColour, deltaRow, deltaCol)) == false) {//received move is not a legal move
printf("%c player wins.", comColour);
validMove = false;
}
}
while ((checkAvailableMove(comColour, N, board, deltaRow, deltaCol)
|| checkAvailableMove(playerColour, N, board, deltaRow, deltaCol)) && validMove) {//one of computer and player has available moves
if (checkAvailableMove(comColour, N, board, deltaRow, deltaCol)) {//if computer has available move, it give the best move among all available moves
comMove(board, N, comColour, deltaRow, deltaCol);
printBoard(board, N);
} else if (checkAvailableMove(comColour, N, board, deltaRow, deltaCol) == false) {
printf("%c player has no valid move.\n", comColour);
}
if (checkAvailableMove(playerColour, N, board, deltaRow, deltaCol)) {//if player has available move,program receives a input move
printf("Enter move for colour %c (RowCol): ", playerColour);
scanf(" %c", &playerRow);
scanf(" %c", &playerCol);
if ((positionInBounds(N, playerRow, playerCol)
&& checkLegalIndirection(board, N, playerRow, playerCol, playerColour, deltaRow, deltaCol)
&& validMove) == true) {//received move is a legal move
changeAndFlip(board, playerColour, playerRow, playerCol, N, deltaRow, deltaCol);
printBoard(board, N);
} else if ((positionInBounds(N, playerRow, playerCol) &&
checkLegalIndirection(board, N, playerRow, playerCol, playerColour, deltaRow, deltaCol)) == false) {//received move is not a legal move
printf("Invalid move.\n");
printf("%c player wins.", comColour);
validMove = false;
}
}
}
if (validMove == true)
determineWiner(board, N);
return (EXIT_SUCCESS);
}
#lab 7 part 2
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
static const double lastRound = 16;
static const double lastTwoRound = 8;
static const double lastThreeRound = 4;
void getScoreBoard(int scoreBoard[26][26], int N, char board[26][26], char colour, int deltaRow, int deltaCol);
void evaluateProrityMove(char board[26][26], int N, char colour, int deltaRow, int deltaCol);
bool oppositeCorner(char board[26][26], char colour, int N, int rowNumber, int colNumber, int deltaRow, int deltaCol);
int bestCorner(char board[26][26], int N, char colour, int deltaRow, int deltaCol);
bool cornerExistCheck(char board[26][26], int N, char colour, int deltaRow, int deltaCol);
bool positionInBounds(int N, char row, char col);
bool checkLegalIndirection(char board[26][26], int N, char row, char col, char colour, int deltaRow, int deltaCol);
void printBoard(char board[26][26], int N);
void changeBoard(char board[26][26], char colour, char row, char col);
void initinBoard(char board[26][26], int N);
bool checkAvailableMove(char colour, int N, char board[26][26], int deltaRow, int deltaCol);
void changeAndFlip(char board[26][26], char colour, char row, char col, int N, int deltaRow, int deltaCol);
int countFlip(char board[26][26], char colour, char row, char col, int N, int deltaRow, int deltaCol);
void cornerCheckAndScoreCount(char board[26][26], int scoreBoard[26][26], int N, char colour, int deltaRow, int deltaCol);
void lastRoundCheck(char board[26][26], int scoreBoard[26][26], int N, char colour, int deltaRow, int deltaCol);
int oppositeLastRound(char board[26][26], char colour, int N, int rowNumber, int colNumber, int deltaRow, int deltaCol);
void lastTwoRoundCheck(char board[26][26], int scoreBoard[26][26], int N, char colour, int deltaRow, int deltaCol);
int oppositeLastTwoRound(char board[26][26], char colour, int N, int rowNumber, int colNumber, int deltaRow, int deltaCol);
void lastThreeRoundCheck(char board[26][26], int scoreBoard[26][26], int N, char colour, int deltaRow, int deltaCol);
int oppositeLastThreeRound(char board[26][26], char colour, int N, int rowNumber, int colNumber, int deltaRow, int deltaCol);
void coreStrategy(char board[26][26], int scoreBoard[26][26], int N, char colour, int deltaRow, int deltaCol);
int oppositeAvailableMoveCount(char board[26][26], int N, char colour, int deltaRow, int deltaCol, char row, char col);
void coreStrategy(char board[26][26], int scoreBoard[26][26], int N, char colour, int deltaRow, int deltaCol) {
int row, col;
char diffColour = 'B' + 'W' - colour;
int middleNumber;
for (row = 0; row < N; row++)
for (col = 0; col < N; col++) {
if (scoreBoard[row][col] != 0) {
middleNumber = oppositeAvailableMoveCount(board, N, colour, deltaRow, deltaCol, row + 'a', col + 'a');
scoreBoard[row][col] = scoreBoard[row][col] - middleNumber;
}
}
}
int oppositeAvailableMoveCount(char board[26][26], int N, char colour, int deltaRow, int deltaCol, char row, char col) {
int r, c, totalNumber = 0;
char fakeBoard[26][26];
char diffColour = 'B' + 'W' - colour;
for (r = 0; r < N; r++)
for (c = 0; c < N; c++)
fakeBoard[r][c] = board[r][c];
changeAndFlip(fakeBoard, colour, row, col, N, deltaRow, deltaCol);
for (r = 0; r < N; r++)
for (c = 0; c < N; c++)
if (checkLegalIndirection(fakeBoard, N, r + 'a', c + 'a', diffColour, deltaRow, deltaCol) == true)
totalNumber++;
return totalNumber;
}
void lastRoundCheck(char board[26][26], int scoreBoard[26][26], int N, char colour, int deltaRow, int deltaCol) {
int rowNumber, colNumber;
for (rowNumber = 0; rowNumber < N; rowNumber++)
for (colNumber = 0; colNumber < N; colNumber++) {
if (rowNumber == 0 || rowNumber == N - 1 || colNumber == 0 || colNumber == N - 1)
scoreBoard[rowNumber][colNumber] == scoreBoard[rowNumber][colNumber]*(lastRound / oppositeLastRound(board, colour, N, rowNumber, colNumber, deltaRow, deltaCol)); //if the available move is on side
}
}
void lastThreeRoundCheck(char board[26][26], int scoreBoard[26][26], int N, char colour, int deltaRow, int deltaCol) {
int rowNumber, colNumber;
for (rowNumber = 0; rowNumber < N; rowNumber++)
for (colNumber = 0; colNumber < N; colNumber++) {
if (rowNumber == 2 || rowNumber == N - 3 || colNumber == 2 || colNumber == N - 3)
scoreBoard[rowNumber][colNumber] == scoreBoard[rowNumber][colNumber]*(lastThreeRound / oppositeLastThreeRound(board, colour, N, rowNumber, colNumber, deltaRow, deltaCol)); //if the available move is on side
}
}
int oppositeLastThreeRound(char board[26][26], char colour, int N, int rowNumber, int colNumber, int deltaRow, int deltaCol) {
char fakeBoard[26][26];
int rowNum, colNum;
int responseNum = 0;
char diffColour = 'B' + 'W' - colour;
for (rowNum = 0; rowNum < N; rowNum++)
for (colNum = 0; colNum < N; colNum++) {
fakeBoard[rowNum][colNum] = board[rowNum][colNum];
}//copy fakeBoard with the current board
changeAndFlip(fakeBoard, colour, (char) rowNumber + 'a', (char) colNumber + 'a', N, deltaRow, deltaCol);
for (rowNum = 0; rowNum < N; rowNum++)
for (colNum = 0; colNum < N; colNum++) {
if (checkLegalIndirection(board, N, rowNum + 'a', colNum + 'a', diffColour, deltaRow, deltaCol) == true)
if (rowNumber == 2 || rowNumber == N - 3 || colNumber == 2 || colNumber == N - 3)
responseNum++;
}
return responseNum;
}
void lastTwoRoundCheck(char board[26][26], int scoreBoard[26][26], int N, char colour, int deltaRow, int deltaCol) {
int rowNumber, colNumber;
for (rowNumber = 0; rowNumber < N; rowNumber++)
for (colNumber = 0; colNumber < N; colNumber++) {
if (rowNumber == 1 || rowNumber == N - 2 || colNumber == 1 || colNumber == N - 2)
scoreBoard[rowNumber][colNumber] == scoreBoard[rowNumber][colNumber]*(oppositeLastTwoRound(board, colour, N, rowNumber, colNumber, deltaRow, deltaCol) / lastTwoRound); //if the available move is on side
}
}
int oppositeLastTwoRound(char board[26][26], char colour, int N, int rowNumber, int colNumber, int deltaRow, int deltaCol) {
char fakeBoard[26][26];
int rowNum, colNum;
int responseNum = 0;
char diffColour = 'B' + 'W' - colour;
for (rowNum = 0; rowNum < N; rowNum++)
for (colNum = 0; colNum < N; colNum++) {
fakeBoard[rowNum][colNum] = board[rowNum][colNum];
}//copy fakeBoard with the current board
changeAndFlip(fakeBoard, colour, (char) rowNumber + 'a', (char) colNumber + 'a', N, deltaRow, deltaCol);
for (rowNum = 0; rowNum < N; rowNum++)
for (colNum = 0; colNum < N; colNum++) {