Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <stdlib.h>
- #include <time.h>
- #define TEMP_MATS (*(temp_row+row_number))
- #define MATS_IN_ROW (*(row+row_number))
- void initializetemp();
- int checkrow(int, int);
- int countfours();
- int counttwos();
- int countones();
- int is_win();
- void display();
- void winning_move(int);
- void losing_move(int);
- int player();
- int computer();
- int row[4] = {7,5,3,1};
- int temp_row[4];
- int counter;
- main()
- {
- printf("\n\n O)) O)) O)) O)) O)) "
- "\n O) O)) O))) O) O)) O)) O)) "
- "\n O)) O)) O O)) O)) O)) O)) O)) O)) O))"
- "\n O)) O)) O)) O)) O)) O)) O)) O)) O)) O)) O)) "
- "\n O)) O) O)) O)) O)O)) O)) O)) O)) O)O)) "
- "\n O)) O)) O)) O)) O)) O)) O)) O)) O)) O)) "
- "\n O)) O)) O)) O)) O)) O))) O)) O))) O)) O))");
- printf("\n\n Welcome to my nim game");
- printf("\n The rules:"
- "\n 1. Matches are laid out like this:\n"
- "\n | | | | | | |"
- "\n | | | | |"
- "\n | | |"
- "\n |\n"
- "\n 2. Players take matches in turns"
- "\n 3. As many as they want"
- "\n 4. But only from one row at a time"
- "\n 5. Whoever takes the last one loses");
- char flag;
- do
- {
- printf("\n\n Press 'y' if you want computer to make the first move... ");
- flag = (char)getchar();
- if(flag == 'Y' || flag == 'y')
- {
- if(computer())
- printf("\n\n YOU WIN!\n\n");
- else
- printf("\n\n YOU LOSE!\n\n");
- }
- else
- {
- display();
- if(player())
- printf("\n\n YOU WIN!\n\n");
- else
- printf("\n\n YOU LOSE!\n\n");
- }
- printf(" Press 'y' to play again... ");
- scanf("%1s",&flag);
- }while(flag=='y' || flag=='Y');
- }
- int player()
- {
- int row_number;
- int how_many;
- printf("\n Your turn. Chose the row and the number of matches you want to take.\n");
- do
- {
- do
- {
- printf("\n Row: ");
- scanf("%d",&row_number);
- if(row_number < 1 || row_number > 4)
- {
- printf("\n ERROR: invalid row number. Please try again.");
- display();
- }
- }while(row_number < 1 || row_number > 4);
- printf(" Matches: ");
- scanf("%d",&how_many);
- if(how_many > *(row+row_number-1) || how_many < 1)
- {
- printf("\n ERROR: invalid number of matches. Please try again.");
- display();
- }
- }while(how_many > *(row+row_number-1) || how_many < 1);
- *(row+row_number-1)-= how_many;
- display();
- return computer();
- }
- int computer()
- {
- int random;
- srand(time(NULL));
- random = rand() % 4;
- if(is_win()==0)
- winning_move(random);
- else
- losing_move(random);
- display();
- if(is_win() == 2)
- return 1;
- else if(is_win() == 3)
- return 0;
- else
- return player();
- }
- int is_win()
- {
- initializetemp();
- int fours = countfours();
- int twos = counttwos();
- int ones = countones();
- if(fours==0 && twos==0)
- {
- if(ones == 0)
- return 2;
- else if(ones == 1)
- return 3;
- else if(ones == 3)
- return 1;
- else
- return 0;
- }
- else if(fours%2 || twos%2 || ones%2)
- return 0;
- else
- return 1;
- }
- void initializetemp()
- {
- for(int i=0;i<4;i++)
- *(temp_row+i) = *(row+i);
- }
- int countfours()
- {
- int fours = 0;
- for(int i=0;i<4;i++)
- { counter = 0;
- fours += checkrow(i,4);
- }
- return fours;
- }
- int counttwos()
- {
- int twos = 0;
- for(int i=0;i<4;i++)
- {counter=0;
- twos += checkrow(i,2);
- }
- return twos;
- }
- int countones()
- {
- int ones = 0;
- for(int i=0;i<4;i++)
- {counter=0;
- ones += checkrow(i,1);
- }
- return ones;
- }
- int checkrow(int row_number, int matches)
- {
- if(TEMP_MATS>=matches)
- {
- TEMP_MATS-= matches;
- counter++;
- return checkrow(row_number, matches);
- }
- else
- return counter;
- }
- void winning_move(int row_number)
- {
- int matches_taken = 0;
- while(MATS_IN_ROW && is_win() == 0)
- {
- MATS_IN_ROW--;
- matches_taken++;
- }
- if(is_win() == 0)
- {
- MATS_IN_ROW = matches_taken;
- winning_move((row_number+1) % 4);
- }
- else
- printf("\n Computer takes %d match(es) from row %d",matches_taken,row_number+1);
- }
- void losing_move(int row_number)
- {
- if(MATS_IN_ROW)
- {
- MATS_IN_ROW--;
- printf("\n Computer takes 1 match(es) from row %d",row_number+1);
- }
- else
- losing_move((row_number+1) % 4);
- }
- void display()
- {
- printf("\n");
- for(int i=0;i<4;i++)
- {
- printf("\n Row %d: ",i+1);
- for(int j=0;j<i;j++)
- printf(" ");
- for(int j=0;j<*(row+i);j++)
- printf("| ");
- }
- printf("\n");
- }
Add Comment
Please, Sign In to add comment