Last question?
Declaring the in declaring the winner the one who picks the last stick will be the looser not the one who have no more to pick..
Any suggestion?
Last question?
Declaring the in declaring the winner the one who picks the last stick will be the looser not the one who have no more to pick..
Any suggestion?
Code:if(sum==1)
Fact - Beethoven wrote his first symphony in C
Or better yet - Move the part where it changes the player's turn around to after theCode:if(sum==0)
Fact - Beethoven wrote his first symphony in C
Try this version of your program:
Code:#include <stdio.h> int main () { char ans; int rows[4]; int i, j, player,rowChosen, sticksChosen, sum; do { rows[0] = 0; rows[1] = 3; rows[2] = 5; rows[3] = 7; player =1; printf("\n\n"); printf("\t\t******************************\t"); printf("\n\t\t****WELCOME TO GAME OF NIM****\t\t\n"); printf("\t\tErvin Ardiente & Orven Dumasig\t\n"); printf("\t\t******************************\t\n\n"); sum = rows[1] + rows[2] + rows[3]; while(sum > 0) { for(i = 1; i <= 3; i++) { if(rows[i]) { printf("\nRow %d:", i); for(j = 0; j < rows[i] ; j++) { printf("\t|"); } printf("\n"); } } printf("\n"); printf("\nPlayer %d's turn! \n\n",player); printf("Please Enter a Row: "); scanf("%d", &rowChosen); while(rowChosen > 3 || rowChosen < 1 || rows[rowChosen] == 0) { printf("Error! Please enter a row with sticks: "); scanf("%d", &rowChosen); } printf("Please Enter Number of sticks to remove: "); scanf("%d", &sticksChosen); while(sticksChosen > rows[rowChosen] || sticksChosen < 1) { printf("Error! Please Enter Number of sticks to remove: "); scanf("%d", &sticksChosen); } rows[rowChosen] -= sticksChosen; sum -= sticksChosen; if(sum == 0) { printf("\n\nPlayer %d You lose!\n\n", player); } player++; if(player == 3) { player = player - 2; } } printf("Do you want to try again? (Y/N) "); scanf(" %c", &ans); }while (ans == 'Y' || ans == 'y'); return 0; }
Oh is this that new game you were talking about adak? haha...
I have this final code anyone check it?
Code:#include <stdio.h> int main () { char ans; do{ int rows[] = {0, 3, 5, 7}; int i, j; int player =1; int rowChosen, sticksChosen; int sum; printf("\n\n"); printf("\t\t******************************\t"); printf("\n\t\t****WELCOME TO GAME OF NIM****\t\t\n"); printf("\t\tErvin Ardiente & Orven Dumasig\t\n"); printf("\t\t******************************\t\n\n"); sum=rows[1]+rows[2]+rows[3]; while(sum>0) { for(i = 1; i <=3; i++) { { printf("\nRow %d:", i); for(j = 0; j < rows[i] ; j++) { printf("\t|"); } printf("\n"); } } printf("\n"); printf("\nPlayer %d's turn! \n\n",player); player++; if(player == 3) { player = player - 2; } printf("Please Enter a Row: "); scanf("%d", &rowChosen); while(rowChosen > 3 || rowChosen < 1 || rows[rowChosen]== 0) { printf("Error! Please enter a row with sticks: "); scanf("%d", &rowChosen); } printf("Please Enter Number of sticks to remove: "); scanf("%d", &sticksChosen); while(sticksChosen > rows[rowChosen] || sticksChosen < 1) { printf("Error! Please Enter Number of sticks to remove again: "); scanf("%d", &sticksChosen); } rows[rowChosen] -= sticksChosen; sum -= sticksChosen; if(sum==0) { printf("\n\nPlayer %d You Win!!\n\n", player); } } printf("Do you want to try again? (Y/N)"); scanf(" %c", &ans); }while (ans == 'Y' || ans == 'y'); return 0;
@ Adak and Click_me! Thank you so much for your help! Have a blessed day! Thank you thank you thank you!
I wouldn't be so sure. You can play it online: pearls before swine
Code:#include <stdio.h> #define ARRAY_SIZE 3 int MyArray[ARRAY_SIZE] = { 2, 4, 6}; int print() { int result=0; int i,j; for(i=0;i<ARRAY_SIZE;i++) { printf("Row %d:", i+1); for(j=0;j<MyArray[i];j++) { printf(" |"); } printf("\n"); fflush(stdout); } return 0; } int sum() { int result=0; int i; for(i=0;i<ARRAY_SIZE;i++) { printf("MyArray[ %d ] = %d\n", i, MyArray[i]); result += MyArray[i]; } return result; } main() { int row, sticks; int player=2; while(sum() != 0) { player = (player == 1) ? 2: 1; print(); printf("\nPlayer %d plays\n", player); printf("row:"); fflush(stdout); scanf("%d", &row); printf("sticks:"); fflush(stdout); scanf("%d", &sticks); if( (row <= 0) || (row > ARRAY_SIZE) ) { printf("ERROR: row %d is invalid\n", row); player = (player == 1) ? 2: 1; continue; } row -= 1; if ( (sticks==0) || (MyArray[row] < sticks) ) { printf("ERROR: sticks %d is invalid\n", sticks); player = (player == 1) ? 2: 1; continue; } MyArray[row] -= sticks; } printf("Player %d Lost\n", player); }
Last edited by garbag; 10-05-2012 at 01:56 PM.