You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 

127 lines
2.8 KiB

  1. #include <stdio.h>
  2. #include <SDL/SDL.h>
  3. #include <stdlib.h>
  4. #include <time.h>
  5. #include "api.h"
  6. #include "gui.h"
  7. int main(void) {
  8. struct game_state_t gamestate = {
  9. { 1,
  10. 1,1,
  11. 1,1,1,
  12. 1,1,1,1,
  13. 6,6,6,6,0,0,0,0,0,2,2,2,2,
  14. 6,6,6,0,0,0,0,0,0,2,2,2,
  15. 6,6,0,0,0,0,0,0,0,2,2,
  16. 6,0,0,0,0,0,0,0,0,2,
  17. 0,0,0,0,0,0,0,0,0,
  18. 5,0,0,0,0,0,0,0,0,3,
  19. 5,5,0,0,0,0,0,0,0,3,3,
  20. 5,5,5,0,0,0,0,0,0,3,3,3,
  21. 5,5,5,5,0,0,0,0,0,3,3,3,3,
  22. 4,4,4,4,
  23. 4,4,4,
  24. 4,4,
  25. 4
  26. }
  27. };
  28. struct gui_resource_t gui_res;
  29. gui_res = display_start(&gui_res);
  30. display_render_board(&gui_res, &gamestate, 1, "Name");
  31. int quit = 0;
  32. /* un peu d'interactivité */
  33. SDL_Event event;
  34. if( display_animsg(&gui_res, &gamestate, 1, "Name", "Click to start!", 1200/*ms*/) )
  35. quit = 1;
  36. while(!quit) {
  37. SDL_WaitEvent(&event);
  38. if(event.type == SDL_QUIT) {
  39. quit = 1;
  40. break;
  41. }
  42. if(event.type == SDL_MOUSEBUTTONDOWN)
  43. break;
  44. }
  45. srand(time(NULL));
  46. int pawn1, pawn2, r, i, n;
  47. enum hole_t player1 = 1, player2 = player1;
  48. while(!quit) {
  49. while(player2 == player1)
  50. player2 = (rand()%6)+1;
  51. while(gamestate.board[pawn2 = rand()%121]); /* trouver un trou vide */
  52. pawn1 = pawn2;
  53. /* un pion du joueur courant au hasard. s'il n'y en a pas on déplacera une case vide sur elle-même car pawn1 = pawn2 */
  54. r = rand()%10;
  55. for(n = i = 0; i < 121; i++) {
  56. if(gamestate.board[i] == player1) {
  57. if(n == r) {
  58. pawn1 = i;
  59. break;
  60. }
  61. n++;
  62. }
  63. }
  64. struct move_t user_move;
  65. int moremoves;
  66. if(player1 == 1) {
  67. if( display_animsg(&gui_res, &gamestate, player1, "Name", "Your turn!", 1200/*ms*/) ) {
  68. quit = 1;
  69. break;
  70. }
  71. do {
  72. display_animsg(&gui_res, &gamestate, player1, "Name", "Move a pawn or right-click", 1200/*ms*/);
  73. if( display_usermove_pawn(&gui_res, &gamestate, player1, "Name", &user_move, &moremoves) ) {
  74. quit = 1;
  75. break;
  76. }
  77. if(moremoves) {
  78. gamestate.board[user_move.endPos] = gamestate.board[user_move.startPos];
  79. gamestate.board[user_move.startPos] = none;
  80. }
  81. } while(moremoves);
  82. if(quit)
  83. break;
  84. if( display_animsg(&gui_res, &gamestate, player1, "Name", "You’ve done your move!", 1200/*ms*/) ) {
  85. quit = 1;
  86. break;
  87. }
  88. } else {
  89. if( display_animove_pawn(&gui_res, gamestate, player1, "Name", pawn1, pawn2) ) {
  90. quit = 1;
  91. break;
  92. }
  93. gamestate.board[pawn2] = gamestate.board[pawn1];
  94. gamestate.board[pawn1] = none;
  95. }
  96. if( display_anirotate_board(&gui_res, &gamestate, player1, "Name", player2, "Name2") ) {
  97. quit = 1;
  98. break;
  99. }
  100. if( display_animsg(&gui_res, &gamestate, player2, "Name2", "Next player’s turn!", 1200/*ms*/) ) {
  101. quit = 1;
  102. break;
  103. }
  104. player1 = player2;
  105. }
  106. display_close(&gui_res);
  107. return 0;
  108. }