Advertisement
Guest User

Untitled

a guest
Dec 18th, 2017
74
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 12.72 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <time.h>
  4. #include <machine/int86.h>
  5. #include <minix/syslib.h>
  6. #include <math.h>
  7.  
  8. #include "jogo.h"
  9. #include "i8254.h"
  10. #include "i8042.h"
  11. #include "timer.h"
  12. #include "kbd.h"
  13. #include "video_gr.h"
  14. #include "read_xpm.h"
  15. #include "sprite.h"
  16. #include "pixmap.h"
  17. #include "lmlib.h"
  18.  
  19.  
  20. unsigned long squareColors[]={11,18,36,54,61};
  21.  
  22. Board board;
  23.  
  24.  
  25.  
  26. //whole game
  27. int game(){
  28.  
  29. draw_board();
  30. algorithm(board.squareArray[0][0].color);
  31.  
  32. unsigned long received_key = 0;
  33. int ipc_status;
  34. message msg;
  35. int irq_set_kbd = kbd_subscribe_int();
  36. int message_request;
  37.  
  38. if (irq_set_kbd == -3) { //subscription of the keyboard
  39. printf("Error at subscribing the keyboard.\n");
  40. return 1;
  41. }
  42. int plays = 0;
  43.  
  44. while (plays != 20) {
  45.  
  46. // driver_receive receives messages (notifications included)
  47. // driver_receive (sender of the msgs, address of message, address of int)
  48. message_request = driver_receive(ANY, &msg, &ipc_status);
  49. if (message_request != 0) {
  50. printf("Driver_receive failed with: %d\n", message_request);
  51. continue;
  52. }
  53.  
  54. if (is_ipc_notify(ipc_status)) { //is_ipc_notify() == true if message received is a notification
  55.  
  56. switch (_ENDPOINT_P(msg.m_source)) { //m_source contains the endpoint of the sender of the message.
  57. case HARDWARE: //value that indicates a hardware interrupt
  58.  
  59. if (msg.NOTIFY_ARG & irq_set_kbd) { //subscribed
  60. kbd_outbuffer(&received_key); //reads the keys
  61.  
  62. int i = atmButton();
  63.  
  64. switch (received_key) {
  65.  
  66. case LEFT_KEY:
  67. if (i > 0) {
  68.  
  69. board.buttonsArray[i].belongFlag = 0;
  70. removeButtonFrame(i);
  71. board.buttonsArray[i - 1].belongFlag = 1;
  72. drawButtonFrame();
  73. }
  74. break;
  75.  
  76. case RIGHT_KEY:
  77. if (i < 4) {
  78.  
  79. board.buttonsArray[i].belongFlag = 0;
  80. removeButtonFrame(i);
  81. board.buttonsArray[i + 1].belongFlag = 1;
  82. drawButtonFrame();
  83. }
  84. break;
  85.  
  86. case ENTER_KEY:
  87.  
  88. algorithm(board.buttonsArray[i].color);
  89. plays++;
  90.  
  91. nrOfPlays(plays);
  92.  
  93. break;
  94.  
  95. }
  96. }
  97.  
  98. break;
  99. default:
  100. break; //no other notifications expected: do nothing
  101. }
  102. } else { //received a standard message, not a notification
  103. // no standard messages expected: do nothing
  104. printf("Not a notification.\n");
  105. }
  106. }
  107.  
  108. if (kbd_unsubscribe_int() != 0) { // unsubscription of the keyboard
  109. printf("Error at unsubscribing.\n");
  110. }
  111.  
  112. return 0;
  113. }
  114.  
  115.  
  116.  
  117.  
  118. //draws the full board using the functions below
  119. Board draw_board(){
  120.  
  121. Frame frame1;
  122. frame1.x = 265;
  123. frame1.y = 100;
  124. frame1.size = 410;
  125. board.frame = frame1;
  126.  
  127. draw_frame(frame1.x, frame1.y, frame1.size);
  128.  
  129. board.x = frame1.x + 5;
  130. board.y = frame1.y + 5;
  131. board.size = frame1.size-10;
  132. unsigned short square_size = 40;
  133. unsigned short temp_x = 0;
  134. unsigned short temp_y = 0;
  135. int i = 0;
  136. int j = 0;
  137.  
  138.  
  139. while(j < 10){
  140.  
  141. while(i < 10){
  142.  
  143. Square square;
  144.  
  145. if((i == 0) && (j == 0))
  146. square.belongFlag = 1;
  147. else
  148. square.belongFlag = 0;
  149.  
  150. square.x = temp_x + board.x;
  151. square.y = temp_y + board.y;
  152. square.size = square_size;
  153. square.color = squareColors[randomColor(5)];
  154.  
  155. board.squareArray[j][i] = square;
  156.  
  157. draw_square(square.x, square.y, square.size, square.color);
  158. temp_x += square_size;
  159. i++;
  160. }
  161.  
  162. temp_x = 0;
  163. temp_y += square_size;
  164.  
  165. j++;
  166. i = 0;
  167. }
  168.  
  169. temp_y += 40 + board.y;
  170. temp_x = board.x;
  171. drawButtons(temp_x + 20, temp_y);
  172. drawButtonFrame();
  173.  
  174.  
  175.  
  176. showXPM(number0,265,10);
  177. showXPM(number2,339,10);
  178. showXPM(number0,387,10);
  179. showXPM(bar_xpm,315,10);
  180. return board;
  181. }
  182.  
  183. //chooses a random color from the squareColors array
  184. unsigned long randomColor(unsigned long n_colors){
  185. return rand() % n_colors;
  186. }
  187.  
  188. //draws a square at the right position, with a certain size and color
  189. int draw_square(unsigned short frame_x, unsigned short frame_y, unsigned short size, unsigned long color){
  190.  
  191. unsigned short temp_x = 0;
  192. unsigned short temp_y = 0;
  193.  
  194. while(temp_y <= size){
  195.  
  196. while(temp_x <= size){
  197.  
  198. set_pixel_color(temp_x + frame_x, temp_y + frame_y, color);
  199. temp_x++;
  200. }
  201.  
  202. temp_x = 0;
  203. temp_y++;
  204. }
  205.  
  206. return 0;
  207. }
  208.  
  209. //draws the white frame behind the keyboard
  210. int draw_frame(unsigned short frame_x, unsigned short frame_y, unsigned short size){
  211.  
  212. unsigned short temp_x = 0;
  213. unsigned short temp_y = 0;
  214.  
  215. while(temp_y <= size){
  216.  
  217. while(temp_x <= size){
  218. set_pixel_color(temp_x + frame_x, temp_y + frame_y, 63);
  219. temp_x ++;
  220. }
  221.  
  222. temp_x = 0;
  223. temp_y++;
  224. }
  225.  
  226. return 0;
  227. }
  228.  
  229. //draws the 5 color buttons
  230. void drawButtons(unsigned short x, unsigned short y) {
  231.  
  232. int i = 0;
  233. unsigned short temp_x = x;
  234.  
  235.  
  236. while(i < (sizeof (squareColors) / sizeof (long))){
  237.  
  238. Square square;
  239.  
  240. if (i == 0)
  241. square.belongFlag = 1;
  242. else
  243. square.belongFlag = 0;
  244.  
  245. square.x = temp_x;
  246. square.y = y;
  247. square.size = 40;
  248. square.color = squareColors[i];
  249.  
  250. draw_square(square.x, square.y, square.size, square.color);
  251. board.buttonsArray[i] = square;
  252.  
  253. temp_x += 80;
  254. i++;
  255. }
  256.  
  257. }
  258.  
  259. //draws a frame in the button we are at the moment
  260. void drawButtonFrame(){
  261.  
  262. int i = atmButton();
  263.  
  264. unsigned short frame_x = board.buttonsArray[i].x;
  265. unsigned short frame_y = board.buttonsArray[i].y;
  266. draw_square(frame_x - 5, frame_y - 5, 50, 63);
  267. draw_square(frame_x, frame_y, 40, board.buttonsArray[i].color);
  268. }
  269.  
  270. //removes the frame from the last button we were at
  271. void removeButtonFrame(int i){
  272.  
  273. unsigned short frame_x = board.buttonsArray[i].x;
  274. unsigned short frame_y = board.buttonsArray[i].y;
  275. draw_square(frame_x - 5, frame_y - 5, 50, 0);
  276. draw_square(frame_x, frame_y, 40, board.buttonsArray[i].color);
  277. }
  278.  
  279. //returns the button we are at the moment
  280. int atmButton(){
  281.  
  282. int i = 0;
  283. int flag = 0;
  284.  
  285. while (flag != 1) {
  286.  
  287. if (board.buttonsArray[i].belongFlag == 1)
  288. flag = 1;
  289. else
  290. i++;
  291. }
  292.  
  293. return i;
  294. }
  295.  
  296. //algorithm to paint
  297. void algorithm(unsigned long color) {
  298.  
  299.  
  300. int i = 0;
  301. int j = 0;
  302.  
  303. while (j < 10) {
  304.  
  305. while (i < 10) {
  306.  
  307. if (board.squareArray[j][i].belongFlag == 1) {
  308.  
  309. if (j == 0) { //uppermost line
  310.  
  311. if (i == 0) { //upper leftmost square
  312.  
  313. if ((board.squareArray[j][i + 1].belongFlag == 0) && (board.squareArray[j][i + 1].color == color)) {
  314. board.squareArray[j][i + 1].belongFlag = 1;
  315. }
  316. if ((board.squareArray[j + 1][i].belongFlag == 0) && (board.squareArray[j + 1][i].color == color)) {
  317. board.squareArray[j + 1][i].belongFlag = 1;
  318. }
  319. }
  320. else if (i == 10) { //upper rightmost square
  321.  
  322. if ((board.squareArray[j][i - 1].belongFlag == 0) && (board.squareArray[j][i - 1].color == color)) {
  323. board.squareArray[j][i - 1].belongFlag = 1;
  324. }
  325. if ((board.squareArray[j + 1][i].belongFlag == 0) && (board.squareArray[j + 1][i].color == color)) {
  326. board.squareArray[j + 1][i].belongFlag = 1;
  327. }
  328. }
  329. else { //rest of uppermost squares
  330.  
  331. if ((board.squareArray[j][i - 1].belongFlag == 0) && (board.squareArray[j][i - 1].color == color)) {
  332. board.squareArray[j][i - 1].belongFlag = 1;
  333. }
  334. if ((board.squareArray[j][i + 1].belongFlag == 0) && (board.squareArray[j][i + 1].color == color)) {
  335. board.squareArray[j][i + 1].belongFlag = 1;
  336. }
  337. if ((board.squareArray[j + 1][i].belongFlag == 0) && (board.squareArray[j + 1][i].color == color)) {
  338. board.squareArray[j + 1][i].belongFlag = 1;
  339. }
  340. }
  341. }
  342.  
  343. else if (j == 10) { //bottom line
  344.  
  345. if (i == 0) { //bottom leftmost square
  346.  
  347. if ((board.squareArray[j][i + 1].belongFlag == 0) && (board.squareArray[j][i + 1].color == color)) {
  348. board.squareArray[j][i + 1].belongFlag = 1;
  349. }
  350. if ((board.squareArray[j - 1][i].belongFlag == 0) && (board.squareArray[j - 1][i].color == color)) {
  351. board.squareArray[j - 1][i].belongFlag = 1;
  352. }
  353. }
  354. else if (i == 10) { //bottom rightmost square
  355.  
  356. if ((board.squareArray[j][i - 1].belongFlag == 0) && (board.squareArray[j][i - 1].color == color)) {
  357. board.squareArray[j][i - 1].belongFlag = 1;
  358. }
  359. if ((board.squareArray[j - 1][i].belongFlag == 0) && (board.squareArray[j - 1][i].color == color)) {
  360. board.squareArray[j - 1][i].belongFlag = 1;
  361. }
  362. }
  363. else { //rest of bottom squares
  364.  
  365. if ((board.squareArray[j][i - 1].belongFlag == 0) && (board.squareArray[j][i - 1].color == color)) {
  366. board.squareArray[j][i - 1].belongFlag = 1;
  367. }
  368. if ((board.squareArray[j][i + 1].belongFlag == 0) && (board.squareArray[j][i + 1].color == color)) {
  369. board.squareArray[j][i + 1].belongFlag = 1;
  370. }
  371. if ((board.squareArray[j - 1][i].belongFlag == 0) && (board.squareArray[j - 1][i].color == color)) {
  372. board.squareArray[j - 1][i].belongFlag = 1;
  373. }
  374. }
  375. }
  376. else{ //rest of squares
  377.  
  378. if(i == 0){ //leftmost squares
  379.  
  380. if ((board.squareArray[j-1][i].belongFlag == 0) && (board.squareArray[j-1][i].color == color)) {
  381. board.squareArray[j-1][i].belongFlag = 1;
  382. }
  383. if ((board.squareArray[j+1][i].belongFlag == 0) && (board.squareArray[j+1][i].color == color)) {
  384. board.squareArray[j+1][i].belongFlag = 1;
  385. }
  386. if ((board.squareArray[j][i+1].belongFlag == 0) && (board.squareArray[j][i+1].color == color)) {
  387. board.squareArray[j][i+1].belongFlag = 1;
  388. }
  389. }
  390. else if(i == 10){ //rightmost squares
  391.  
  392. if ((board.squareArray[j-1][i].belongFlag == 0) && (board.squareArray[j-1][i].color == color)) {
  393. board.squareArray[j-1][i].belongFlag = 1;
  394. }
  395. if ((board.squareArray[j+1][i].belongFlag == 0) && (board.squareArray[j+1][i].color == color)) {
  396. board.squareArray[j+1][i].belongFlag = 1;
  397. }
  398. if ((board.squareArray[j][i-1].belongFlag == 0) && (board.squareArray[j][i-1].color == color)) {
  399. board.squareArray[j][i-1].belongFlag = 1;
  400. }
  401. }
  402. else{ //squares surrounded by other squares
  403.  
  404. if ((board.squareArray[j-1][i].belongFlag == 0) && (board.squareArray[j-1][i].color == color)) {
  405. board.squareArray[j-1][i].belongFlag = 1;
  406. }
  407. if ((board.squareArray[j+1][i].belongFlag == 0) && (board.squareArray[j+1][i].color == color)) {
  408. board.squareArray[j+1][i].belongFlag = 1;
  409. }
  410. if ((board.squareArray[j][i-1].belongFlag == 0) && (board.squareArray[j][i-1].color == color)) {
  411. board.squareArray[j][i-1].belongFlag = 1;
  412. }
  413. if ((board.squareArray[j][i+1].belongFlag == 0) && (board.squareArray[j][i+1].color == color)) {
  414. board.squareArray[j][i+1].belongFlag = 1;
  415. }
  416. }
  417. }
  418. }
  419. i++;
  420. }
  421.  
  422. i = 0;
  423. j++;
  424. }
  425.  
  426. colorsAllFlags(color);
  427. }
  428.  
  429. //colors all squares that have the flag on
  430. void colorsAllFlags(unsigned long color) {
  431.  
  432. int i = 0;
  433. int j = 0;
  434.  
  435. while (j < 10) {
  436. while (i < 10) {
  437.  
  438. if (board.squareArray[j][i].belongFlag == 1){
  439. board.squareArray[j][i].color = color;
  440. draw_square(board.squareArray[j][i].x, board.squareArray[j][i].y, board.squareArray[j][i].size-1, color);
  441. }
  442. i++;
  443. }
  444. j++;
  445. i = 0;
  446. }
  447. }
  448.  
  449. //prints a xpm
  450. void showXPM(char *xpm[],unsigned short x, unsigned short y ){
  451.  
  452.  
  453. int width, height;
  454. char *sprite, *color;
  455.  
  456. sprite = read_xpm(xpm, &width, &height);
  457. unsigned short temp_x = 0;
  458. unsigned short temp_y = 0;
  459.  
  460. while(temp_y < height){
  461.  
  462. while(temp_x < width){
  463.  
  464. color = sprite + temp_x + width * temp_y;
  465. set_pixel_color(temp_x + x, temp_y + y, *color);
  466. temp_x++;
  467. }
  468.  
  469. temp_y++;
  470. temp_x = 0;
  471. }
  472.  
  473. }
  474.  
  475. void nrOfPlays(int plays) {
  476. switch (plays){
  477. case 1 :
  478. showXPM(number1, 265, 10);
  479. break;
  480. case 2:
  481. showXPM(number2, 265, 10);
  482. break;
  483. case 3:
  484. showXPM(number3, 265, 10);
  485. break;
  486. case 4:
  487. showXPM(number4, 265, 10);
  488. break;
  489. case 5:
  490. showXPM(number5, 265, 10);
  491. break;
  492. case 6:
  493. showXPM(number6, 265, 10);
  494. break;
  495. case 7:
  496. showXPM(number7, 265, 10);
  497. break;
  498. case 8:
  499. showXPM(number8, 265, 10);
  500. break;
  501. case 9:
  502. showXPM(number9, 265, 10);
  503. break;
  504. case 10:
  505. showXPM(number1, 265, 10);
  506. showXPM(number0, 313, 10);
  507. showXPM(number2, 385, 10);
  508. showXPM(number0, 433, 10);
  509. showXPM(bar_xpm, 361, 10);
  510.  
  511. break;
  512. case 11:
  513. showXPM(number1, 313, 10);
  514. break;
  515. case 12:
  516. showXPM(number2, 313, 10);
  517. break;
  518. case 13:
  519. showXPM(number3, 313, 10);
  520. break;
  521. case 14:
  522. showXPM(number4, 313, 10);
  523. break;
  524. case 15:
  525. showXPM(number5, 313, 10);
  526. break;
  527. case 16:
  528. showXPM(number6, 313, 10);
  529. break;
  530. case 17:
  531. showXPM(number7, 313, 10);
  532. break;
  533. case 18:
  534. showXPM(number8, 313, 10);
  535. break;
  536. case 19:
  537. showXPM(number9, 313, 10);
  538. break;
  539. case 20:
  540. showXPM(number2, 265, 10);
  541. showXPM(number0, 313, 10);
  542. break;
  543.  
  544. }
  545. }
  546. ;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement