Advertisement
Guest User

Untitled

a guest
May 27th, 2015
230
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 11.95 KB | None | 0 0
  1. #include<stdio.h>
  2. #define ROW_NUM 6
  3. #define COLS_NUM 7
  4.  
  5.  
  6. //function that prints the board
  7. void board_print(char board[ROW_NUM][COLS_NUM]){
  8. int i=0,j=0;
  9. for(i=0;i<ROW_NUM;i++){
  10. for(j=0;j<COLS_NUM;j++){
  11. printf("|%c",board[i][j]);
  12. }
  13. printf("|");
  14. printf("\n");
  15. }
  16. for(i=0;i<2*COLS_NUM+1;i++){
  17. printf("-");
  18. }
  19. printf("\n");
  20. for(i=1;i<COLS_NUM+1;i++){
  21. printf(" %d",i);
  22. }
  23. printf("\n");
  24. }
  25.  
  26.  
  27. //function that takes the column number from the player
  28. void getting_token(int Col_Mem[1],int scanf_val[1]){
  29. int col_num=0;
  30. scanf_val[0]=scanf("%d",&col_num);
  31. Col_Mem[0]=col_num;
  32. }
  33.  
  34.  
  35. //function that checks the range
  36. int check_range(int Col_Mem[1],int scanf_val[1]){
  37. if((Col_Mem[0]<0)||(Col_Mem[0]>COLS_NUM)){
  38. printf("Invalid move. Input move is out of range.\n");
  39. printf("Please try again:\n");
  40. return 1;
  41. }
  42. if(scanf_val[0]!=1){
  43. printf("You should have entered a number! We are doomed...\n");
  44. }
  45. return 0;
  46. }
  47.  
  48.  
  49. //function that flips the turn of the player to play
  50. void player_turn(int z[1]){
  51. z[0]=1-z[0];
  52. if(z[0]){
  53. printf("Player *, in which column would you like to place your");
  54. printf(" token? (enter 0 to undo)\n");
  55. }
  56. else{
  57. printf("Player o, in which column would you like to place your");
  58. printf(" token? (enter 0 to undo)\n");
  59. }
  60. }
  61.  
  62.  
  63. //function that fills the board with tokens,in the right place
  64. int filling_board(char board[ROW_NUM][COLS_NUM],
  65. int z[1],int Col_Mem[1]){
  66. int u=Col_Mem[0];
  67. int i=0,s_counter=0;
  68. for(i=ROW_NUM-1;i>-1;i--){
  69. if((board[i][u-1]!='*')&&(board[i][u-1]!='o')&&(u!=0)){
  70. if(z[0]==1){
  71. board[i][u-1]='*';
  72. s_counter++;
  73. break;
  74. }
  75. else{
  76. board[i][u-1]='o';
  77. s_counter++;
  78. break;
  79. }
  80. }
  81. }
  82. return s_counter;
  83. }
  84.  
  85.  
  86. //function that checks if the column is full
  87. int full_check(char board[ROW_NUM][COLS_NUM],int Col_Mem[1],int z[1]){
  88. int u=Col_Mem[0];
  89. if((board[0][u-1]=='*')||(board[0][u-1]=='o')){
  90. printf("Invalid move. Column %d is full.\n",u);
  91. printf("Please try again:\n");
  92. z[0]=1-z[0];
  93. return 1;
  94. }
  95. return 0;
  96. }
  97.  
  98.  
  99. //undo terms function - checks the terms for undo
  100. int undo_terms(char board[ROW_NUM][COLS_NUM],int Col_Mem[1]){
  101. int i=0,j=0,counter=0;
  102. if(Col_Mem[0]==0){ //if the player enters 0,the function works
  103. for(i=0;i<ROW_NUM;i++){
  104. for(j=0;j<COLS_NUM;j++){
  105. if((board[i][j]!='o')&&(board[i][j]!='*')){counter++;}
  106. }
  107. }
  108. if(counter==42){ //checks if the board is empty
  109. return 1;
  110. }
  111. else{
  112. return 0;
  113. }
  114. }
  115.  
  116. return 2;
  117.  
  118. }
  119.  
  120.  
  121. //undo function- function that do the undo process
  122. void undo_function(char board[ROW_NUM][COLS_NUM],int Moves_Mem[42],
  123. int Moves_loc[1],int b){
  124.  
  125. if(b==0){ //if the player entered 0 and the board isn't empty
  126. Moves_loc[0]-=1; /*we move 1 backwards in the Moves_Mem,to know
  127. the value before the player entered 0 */
  128. int i=0;
  129. int x=Moves_loc[0];
  130. int u=Moves_Mem[x]; /*the value in the right place,where we
  131. want to make the undo */
  132. for(i=0;i<ROW_NUM;i++){
  133. if((board[i][u-1]=='*')||(board[i][u-1]=='o')){
  134. board[i][u-1]=' ';
  135. Moves_loc[0]-=1; /*we move 1 more backwards,because in
  136. the next turn we move 1 forward-by this move backwards
  137. we prevent from moving twice in the Moves_Mem*/
  138. break;
  139. }
  140. }
  141. }
  142. }
  143.  
  144.  
  145. /*undo function-empty board:when the player press undo and the board
  146. is empty*/
  147. int undo_empty_board(int b){
  148. if(b==1){ //if the player entered 0 and the board is empty
  149. printf("Invalid move. Cannot undo when board is empty.\n");
  150. printf("Please try again:\n");
  151. return 1;
  152. }
  153. return 0;
  154. }
  155.  
  156.  
  157. //check if a player wins , in column!!!
  158. int check_winner_col(char board[ROW_NUM][COLS_NUM]){
  159. int i=0,j=0;
  160. /* when identify o-*,checks if there is another 3 token in a row
  161. from the same type */
  162. for(i=ROW_NUM-1;i>ROW_NUM-4;i--){
  163. for(j=0;j<COLS_NUM;j++){
  164. if((board[i][j]==board[i-1][j])&&(board[i-1][j]==board[i-2][j])
  165. &&(board[i-2][j]==board[i-3][j])&&(board[i-3][j]=='*')){
  166. printf("The game has ended! Player * has won!!!!!\n");
  167. return 1;
  168. }
  169. if((board[i][j]==board[i-1][j])&&(board[i-1][j]==board[i-2][j])
  170. &&(board[i-2][j]==board[i-3][j])&&(board[i-3][j]=='o')){
  171. printf("The game has ended! Player o has won!!!!!\n");
  172. return 1;
  173. }
  174. }
  175. }
  176. return 0;
  177. }
  178.  
  179.  
  180. //check if a player wins in row!!!
  181. int check_winner_row(char board[ROW_NUM][COLS_NUM]){
  182. int i=0,j=0;
  183. /* when identify o-*,checks if there is another 3 token in a column
  184. from the same type */
  185. for(i=ROW_NUM-1;i>-1;i--){
  186. for(j=0;j<COLS_NUM-3;j++){
  187. if((board[i][j]==board[i][j+1])&&(board[i][j+1]==board[i][j+2])
  188. &&(board[i][j+2]==board[i][j+3])&&(board[i][j+3]=='*')){
  189. printf("The game has ended! Player * has won!!!!!\n");
  190. return 1;
  191. }
  192. if((board[i][j]==board[i][j+1])&&(board[i][j+1]==board[i][j+2])
  193. &&(board[i][j+2]==board[i][j+3])&&(board[i][j+3]=='o')){
  194. printf("The game has ended! Player o has won!!!!!\n");
  195. return 1;
  196. }
  197. }
  198. }
  199. return 0;
  200. }
  201.  
  202.  
  203. //checks if a player wins in diagonal from left to right
  204. int check_winner_diagonal_lr(char board[ROW_NUM][COLS_NUM]){
  205. int i=0,j=0;
  206. /* when identify o-*,checks if there is another 3 token in a diagonal
  207. from left to right ,from the same type */
  208. for(i=ROW_NUM-1;i>2;i--){
  209. for(j=0;j<COLS_NUM-3;j++){
  210. if((board[i][j]==board[i-1][j+1])&&(board[i-1][j+1]==board[i-2][j+2])
  211. &&(board[i-2][j+2]==board[i-3][j+3])&&(board[i-3][j+3]=='*')){
  212. printf("The game has ended! Player * has won!!!!!\n");
  213. return 1;
  214. }
  215. if((board[i][j]==board[i-1][j+1])&&(board[i-1][j+1]==board[i-2][j+2])
  216. &&(board[i-2][j+2]==board[i-3][j+3])&&(board[i-3][j+3]=='o')){
  217. printf("The game has ended! Player o has won!!!!!\n");
  218. return 1;
  219. }
  220. }
  221. }
  222. return 0;
  223. }
  224.  
  225.  
  226. //checks if a player wins in diagonal from right to left
  227. int check_winner_diagonal_rl(char board[ROW_NUM][COLS_NUM]){
  228. int i=0,j=0;
  229. /* when identify o-*,checks if there is another 3 token in a diagonal
  230. from right to left ,from the same type */
  231. for(i=ROW_NUM-1;i>2;i--){
  232. for(j=COLS_NUM-1;j>2;j--){
  233. if((board[i][j]==board[i-1][j-1])&&(board[i-1][j-1]==board[i-2][j-2])
  234. &&(board[i-2][j-2]==board[i-3][j-3])&&(board[i-3][j-3]=='*')){
  235. printf("The game has ended! Player * has won!!!!!\n");
  236. return 1;
  237. }
  238. if((board[i][j]==board[i-1][j-1])&&(board[i-1][j-1]==board[i-2][j-2])
  239. &&(board[i-2][j-2]==board[i-3][j-3])&&(board[i-3][j-3]=='o')){
  240. printf("The game has ended! Player o has won!!!!!\n");
  241. return 1;
  242. }
  243. }
  244. }
  245. return 0;
  246. }
  247.  
  248.  
  249.  
  250. //function that checks if there is a tie
  251. int tie_function(char board[ROW_NUM][COLS_NUM]){
  252. int i=0,j=0,counter=0;
  253. for(i=0;i<ROW_NUM;i++){
  254. for(j=0;j<COLS_NUM;j++){
  255. if((board[i][j]=='o')||(board[i][j]=='*')){counter++;}
  256. if(counter==42){
  257. printf("The game has ended! It's a tie.\n");
  258. return 1;
  259. }
  260. }
  261. }
  262. return 0;
  263. }
  264.  
  265.  
  266. //open function - starts the program
  267. void open_board_function(char board[ROW_NUM][COLS_NUM]){
  268. printf("Welcome to Connect-4!\n\n");
  269. board_print(board);
  270. printf("Player *, in which column would you like to place your");
  271. printf(" token? (enter 0 to undo)\n");
  272. }
  273.  
  274. //undo combined functions
  275. int undo(char board[ROW_NUM][COLS_NUM],int Col_Mem[1],
  276. int Moves_Mem[42],int Moves_loc[1]){
  277. int b=0;
  278. b=undo_terms(board,Col_Mem);
  279. undo_function(board,Moves_Mem,Moves_loc,b);
  280. if(undo_empty_board(b)){return 1;}
  281. return 0;
  282. }
  283.  
  284.  
  285. //win and tie combined functions
  286. int win_tie(char board[ROW_NUM][COLS_NUM]){
  287. int m=0,t=0,r=0,w=0,s=0;
  288. if((m=check_winner_col(board))==1){return 1;}
  289. if((t=check_winner_row(board))==1){return 1;}
  290. if((r=check_winner_diagonal_lr(board))==1){return 1;}
  291. if((w=check_winner_diagonal_rl(board))==1){return 1;}
  292. if(((s=tie_function(board))==1)&&(m!=1)&&(t!=1)&&(r!=1)&&(w!=1)){
  293. return 1;
  294. }
  295. return 0;
  296. }
  297.  
  298.  
  299. //full column function
  300. int full_col_check(char board[ROW_NUM][COLS_NUM], int z[1],
  301. int Col_Mem[1],int Moves_loc[1]){
  302. int v=0;
  303. if((v=full_check(board,Col_Mem,z))==1){
  304. Moves_loc[0]-=1;
  305. z[0]=1-z[0];
  306. return 1;
  307. }
  308. return 0;
  309. }
  310.  
  311.  
  312. //board combined functions
  313. void board_function(char board[ROW_NUM][COLS_NUM], int z[1],
  314. int Col_Mem[1]){
  315. filling_board(board,z,Col_Mem);
  316. board_print(board);
  317. }
  318.  
  319.  
  320. //function that puts data into the board
  321. void board_first_fill(char board[ROW_NUM][COLS_NUM]){
  322. int i=0,j=0;
  323. for(i=0;i<ROW_NUM;i++){
  324. for(j=0;j<COLS_NUM;j++){
  325. board[i][j]=32;
  326. }
  327. }
  328. }
  329.  
  330.  
  331. int main()
  332. {
  333. char board[ROW_NUM][COLS_NUM]; //array that remembers the board
  334. board_first_fill(board);
  335. open_board_function(board);
  336. int Col_Mem[1]={0},z[1]={1},scanf_val[1]={0},Moves_Mem[42]={0};
  337. int Moves_loc[1]={0};
  338. /* -Col_Mem-it's array that remembers the column number entered by
  339. -a player , along the whole program
  340. -z-array that flips , used in player_turn and another functions
  341. -scanf_val-array that remembers the value that scanf got , used
  342. to know if the input is a number or not
  343. -Moves_Mem-array that remembers the moves played
  344. -Moves_loc-array that remember the location inside the Moves_Mem
  345. array
  346. */
  347. while(1){
  348. getting_token(Col_Mem,scanf_val);
  349. if(check_range(Col_Mem,scanf_val)){continue;}
  350. if(scanf_val[0]!=1){break;}
  351. Moves_Mem[Moves_loc[0]+=1]=Col_Mem[0];
  352. if(full_col_check(board,z,Col_Mem,Moves_loc)){continue;}
  353. if(undo(board,Col_Mem,Moves_Mem,Moves_loc)){continue;}
  354. board_function(board,z,Col_Mem);
  355. if(win_tie(board)){break;}
  356. player_turn(z);
  357.  
  358. }
  359. return 0;
  360. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement