Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- ============================================================================
- Name : cfour.c
- Author :
- Version :
- Copyright : Your copyright notice
- Description : Hello World in C, Ansi-style
- ============================================================================
- */
- #include <stdio.h>
- #include <stdlib.h>
- #include <string.h>
- int score(int *p);
- int player;
- int main(void) {
- int i,j,k,m,n,let;
- FILE *fp;
- char line[14];
- int board[6][7];
- player = 0;
- /* program is player 0 */
- fp = fopen("C:\\Users\\Public\\Connect4\\workspace\\board.txt", "r");
- if (fp == NULL) {
- fprintf(stderr, "Can't open input file board.txt!\n");
- exit(1);
- }
- for (i=0; i<6; i++)
- {
- j=0;
- while (j<7)
- {
- let = fgetc(fp);
- if ((let>47)&&(let<58))
- {
- board[5-i][j] = let - 48;
- j++;
- }
- }
- }
- for (i=0; i<6; i++)
- { for (j=0; j<7; j++)
- printf("%c ", 48 + board[5-i][j]);
- printf("\n");
- }
- m = 0;
- m = score(board);
- m = m+1;
- return EXIT_SUCCESS;
- }
- int score(int *p)
- {
- int i,j,sc,ii, pl, op, free1, free2, runs, adsc;
- int scboard[12][13];
- int s14, s15, s16, s17, s24, s25, s26, s34, s35, s40;
- int ir, dx, dy;
- sc = 0;
- /* for scoring, embed the board into a larger 12x13 board with paddings by 3 on each side */
- for (i=0; i<12; i++)
- { for (j=0; j<13; j++)
- scboard[i][j] = 3;
- }
- for (i=0; i<6; i++)
- { for (j=0; j<7; j++)
- scboard[3+i][3+j] = *(p+7*i+j);
- }
- /* scoring system
- * one in a line of 4: 10pts
- * one in a line of 5: 13pts
- * one in a line of 6: 16pts
- * one in a line of 7: 19pts
- *
- * two in a line of 4: 22pts
- * two in a line of 5: 26pts
- * two in a line of 6: 30pts
- *
- * three in a line of 4: 35pts
- * three in a line of 5: 45pts
- *
- * four: 1000pts
- *
- * */
- s14 = 10; s15 = 13; s16 = 16; s17 = 19;
- s24 = 22; s25 = 26; s26 = 30;
- s34 = 35; s35 = 45;
- s40 = 1000;
- for (ii=0; ii<2; ii++)
- {
- /* two passes of scoring, first for player, then for opponent */
- if (ii == 0) {pl = player; op = 1 - player;}
- else { op = player; pl = 1 - player;}
- printf("player %d \n", pl);
- /* 4 runs: vertical, horizontal and 2 diagonals */
- for (ir = 0; ir < 4; ir++)
- {
- switch (ir)
- {
- case 0: {dx = 0; dy = 1; printf("vertical \n");
- printf("corner %d below %d \n", scboard[3][3], scboard[3-dy][3-dx]);
- } break; /* vertical */
- case 1: {dx = 1; dy = 0; printf("horizontal \n");} break; /* horizontal */
- case 2: {dx = 1; dy = 1; printf("diagonal up \n");} break; /* diagonal up */
- case 3: {dx = 1; dy = -1; printf("diagonal down \n");} break; /* diagonal down */
- }
- printf("dx = %d, dy = %d \n", dx, dy);
- for (j=3; j<10; j++)
- for (i=3; i<9; i++)
- {
- /* check if the beginning of a sequence */
- adsc = 0;
- printf ("position col %d row %d: %d, %d \n", j-3, i-3, scboard[i][j], scboard[i-dy][j-dx]);
- if ((scboard[i][j]==pl)&&(scboard[i-dy][j-dx]!=pl))
- {
- /* check free space before the sequence*/
- printf ("position col %d row %d \n", j-3, i-3);
- free1 = 0;
- if (scboard[i-dy][j-dx]==2)
- {
- free1 = 1;
- if (scboard[i-2*dy][j-2*dx]==2)
- {
- free1 = 2;
- if (scboard[i-3*dy][j-3*dx]==2)
- {
- free1 = 3;
- }
- }
- }
- /* check the length of a sequence */
- runs = 1;
- if (scboard[i+dy][j+dx]==pl)
- {
- runs = 2;
- if (scboard[i+2*dy][j+2*dx]==pl)
- {
- runs = 3;
- if (scboard[i+3*dy][j+3*dx]==pl)
- {
- runs = 4;
- }
- }
- }
- /* check free space after */
- free2 = 0;
- if (scboard[i+dy*runs][j+dx*runs]==2)
- {
- free2 = 1;
- if (scboard[i+dy*(runs+1)][j+dx*(runs+1)]==2)
- {
- free2 = 2;
- if (scboard[i+dy*(runs+2)][j+dx*(runs+2)]==2)
- {
- free2 = 3;
- }
- }
- }
- if (runs == 1)
- {
- if (free1 + free2 == 6) adsc = s17;
- if (free1 + free2 == 5) adsc = s16;
- if (free1 + free2 == 4) adsc = s15;
- if (free1 + free2 == 3) adsc = s14;
- }
- if (runs == 2)
- if (free1 > 2) free1 = 2;
- if (free2 > 2) free2 = 2;
- {
- if (free1 + free2 == 4) adsc = s26;
- if (free1 + free2 == 3) adsc = s25;
- if (free1 + free2 == 2) adsc = s24;
- }
- if (runs == 3)
- if (free1 > 1) free1 = 1;
- if (free2 > 1) free2 = 1;
- {
- if (free1 + free2 == 2) adsc = s35;
- if (free1 + free2 == 1) adsc = s34;
- }
- if (runs == 4) adsc = s40;
- }
- printf("col = %d, row = %d, free1 = %d, runs = %d, free2 = %d \n", j-3, i-3, free1, runs, free2);
- if (adsc > 0) printf(" col %d row %d score %d \n", j-3, i-3, adsc);
- sc += adsc*(1-2*ii);
- }
- }
- }
- return sc;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement