Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <stdlib.h>
- #include <time.h>
- #include <machine/int86.h>
- #include <minix/syslib.h>
- #include <math.h>
- #include "jogo.h"
- #include "i8254.h"
- #include "i8042.h"
- #include "timer.h"
- #include "kbd.h"
- #include "video_gr.h"
- #include "read_xpm.h"
- #include "sprite.h"
- #include "pixmap.h"
- #include "lmlib.h"
- unsigned long squareColors[]={11,18,36,54,61};
- Board board;
- //whole game
- int game(){
- draw_board();
- algorithm(board.squareArray[0][0].color);
- unsigned long received_key = 0;
- int ipc_status;
- message msg;
- int irq_set_kbd = kbd_subscribe_int();
- int message_request;
- if (irq_set_kbd == -3) { //subscription of the keyboard
- printf("Error at subscribing the keyboard.\n");
- return 1;
- }
- int plays = 0;
- while (plays != 20) {
- // driver_receive receives messages (notifications included)
- // driver_receive (sender of the msgs, address of message, address of int)
- message_request = driver_receive(ANY, &msg, &ipc_status);
- if (message_request != 0) {
- printf("Driver_receive failed with: %d\n", message_request);
- continue;
- }
- if (is_ipc_notify(ipc_status)) { //is_ipc_notify() == true if message received is a notification
- switch (_ENDPOINT_P(msg.m_source)) { //m_source contains the endpoint of the sender of the message.
- case HARDWARE: //value that indicates a hardware interrupt
- if (msg.NOTIFY_ARG & irq_set_kbd) { //subscribed
- kbd_outbuffer(&received_key); //reads the keys
- int i = atmButton();
- switch (received_key) {
- case LEFT_KEY:
- if (i > 0) {
- board.buttonsArray[i].belongFlag = 0;
- removeButtonFrame(i);
- board.buttonsArray[i - 1].belongFlag = 1;
- drawButtonFrame();
- }
- break;
- case RIGHT_KEY:
- if (i < 4) {
- board.buttonsArray[i].belongFlag = 0;
- removeButtonFrame(i);
- board.buttonsArray[i + 1].belongFlag = 1;
- drawButtonFrame();
- }
- break;
- case ENTER_KEY:
- algorithm(board.buttonsArray[i].color);
- plays++;
- nrOfPlays(plays);
- break;
- }
- }
- break;
- default:
- break; //no other notifications expected: do nothing
- }
- } else { //received a standard message, not a notification
- // no standard messages expected: do nothing
- printf("Not a notification.\n");
- }
- }
- if (kbd_unsubscribe_int() != 0) { // unsubscription of the keyboard
- printf("Error at unsubscribing.\n");
- }
- return 0;
- }
- //draws the full board using the functions below
- Board draw_board(){
- Frame frame1;
- frame1.x = 265;
- frame1.y = 100;
- frame1.size = 410;
- board.frame = frame1;
- draw_frame(frame1.x, frame1.y, frame1.size);
- board.x = frame1.x + 5;
- board.y = frame1.y + 5;
- board.size = frame1.size-10;
- unsigned short square_size = 40;
- unsigned short temp_x = 0;
- unsigned short temp_y = 0;
- int i = 0;
- int j = 0;
- while(j < 10){
- while(i < 10){
- Square square;
- if((i == 0) && (j == 0))
- square.belongFlag = 1;
- else
- square.belongFlag = 0;
- square.x = temp_x + board.x;
- square.y = temp_y + board.y;
- square.size = square_size;
- square.color = squareColors[randomColor(5)];
- board.squareArray[j][i] = square;
- draw_square(square.x, square.y, square.size, square.color);
- temp_x += square_size;
- i++;
- }
- temp_x = 0;
- temp_y += square_size;
- j++;
- i = 0;
- }
- temp_y += 40 + board.y;
- temp_x = board.x;
- drawButtons(temp_x + 20, temp_y);
- drawButtonFrame();
- showXPM(number0,265,10);
- showXPM(number2,339,10);
- showXPM(number0,387,10);
- showXPM(bar_xpm,315,10);
- return board;
- }
- //chooses a random color from the squareColors array
- unsigned long randomColor(unsigned long n_colors){
- return rand() % n_colors;
- }
- //draws a square at the right position, with a certain size and color
- int draw_square(unsigned short frame_x, unsigned short frame_y, unsigned short size, unsigned long color){
- unsigned short temp_x = 0;
- unsigned short temp_y = 0;
- while(temp_y <= size){
- while(temp_x <= size){
- set_pixel_color(temp_x + frame_x, temp_y + frame_y, color);
- temp_x++;
- }
- temp_x = 0;
- temp_y++;
- }
- return 0;
- }
- //draws the white frame behind the keyboard
- int draw_frame(unsigned short frame_x, unsigned short frame_y, unsigned short size){
- unsigned short temp_x = 0;
- unsigned short temp_y = 0;
- while(temp_y <= size){
- while(temp_x <= size){
- set_pixel_color(temp_x + frame_x, temp_y + frame_y, 63);
- temp_x ++;
- }
- temp_x = 0;
- temp_y++;
- }
- return 0;
- }
- //draws the 5 color buttons
- void drawButtons(unsigned short x, unsigned short y) {
- int i = 0;
- unsigned short temp_x = x;
- while(i < (sizeof (squareColors) / sizeof (long))){
- Square square;
- if (i == 0)
- square.belongFlag = 1;
- else
- square.belongFlag = 0;
- square.x = temp_x;
- square.y = y;
- square.size = 40;
- square.color = squareColors[i];
- draw_square(square.x, square.y, square.size, square.color);
- board.buttonsArray[i] = square;
- temp_x += 80;
- i++;
- }
- }
- //draws a frame in the button we are at the moment
- void drawButtonFrame(){
- int i = atmButton();
- unsigned short frame_x = board.buttonsArray[i].x;
- unsigned short frame_y = board.buttonsArray[i].y;
- draw_square(frame_x - 5, frame_y - 5, 50, 63);
- draw_square(frame_x, frame_y, 40, board.buttonsArray[i].color);
- }
- //removes the frame from the last button we were at
- void removeButtonFrame(int i){
- unsigned short frame_x = board.buttonsArray[i].x;
- unsigned short frame_y = board.buttonsArray[i].y;
- draw_square(frame_x - 5, frame_y - 5, 50, 0);
- draw_square(frame_x, frame_y, 40, board.buttonsArray[i].color);
- }
- //returns the button we are at the moment
- int atmButton(){
- int i = 0;
- int flag = 0;
- while (flag != 1) {
- if (board.buttonsArray[i].belongFlag == 1)
- flag = 1;
- else
- i++;
- }
- return i;
- }
- //algorithm to paint
- void algorithm(unsigned long color) {
- int i = 0;
- int j = 0;
- while (j < 10) {
- while (i < 10) {
- if (board.squareArray[j][i].belongFlag == 1) {
- if (j == 0) { //uppermost line
- if (i == 0) { //upper leftmost square
- if ((board.squareArray[j][i + 1].belongFlag == 0) && (board.squareArray[j][i + 1].color == color)) {
- board.squareArray[j][i + 1].belongFlag = 1;
- }
- if ((board.squareArray[j + 1][i].belongFlag == 0) && (board.squareArray[j + 1][i].color == color)) {
- board.squareArray[j + 1][i].belongFlag = 1;
- }
- }
- else if (i == 10) { //upper rightmost square
- if ((board.squareArray[j][i - 1].belongFlag == 0) && (board.squareArray[j][i - 1].color == color)) {
- board.squareArray[j][i - 1].belongFlag = 1;
- }
- if ((board.squareArray[j + 1][i].belongFlag == 0) && (board.squareArray[j + 1][i].color == color)) {
- board.squareArray[j + 1][i].belongFlag = 1;
- }
- }
- else { //rest of uppermost squares
- if ((board.squareArray[j][i - 1].belongFlag == 0) && (board.squareArray[j][i - 1].color == color)) {
- board.squareArray[j][i - 1].belongFlag = 1;
- }
- if ((board.squareArray[j][i + 1].belongFlag == 0) && (board.squareArray[j][i + 1].color == color)) {
- board.squareArray[j][i + 1].belongFlag = 1;
- }
- if ((board.squareArray[j + 1][i].belongFlag == 0) && (board.squareArray[j + 1][i].color == color)) {
- board.squareArray[j + 1][i].belongFlag = 1;
- }
- }
- }
- else if (j == 10) { //bottom line
- if (i == 0) { //bottom leftmost square
- if ((board.squareArray[j][i + 1].belongFlag == 0) && (board.squareArray[j][i + 1].color == color)) {
- board.squareArray[j][i + 1].belongFlag = 1;
- }
- if ((board.squareArray[j - 1][i].belongFlag == 0) && (board.squareArray[j - 1][i].color == color)) {
- board.squareArray[j - 1][i].belongFlag = 1;
- }
- }
- else if (i == 10) { //bottom rightmost square
- if ((board.squareArray[j][i - 1].belongFlag == 0) && (board.squareArray[j][i - 1].color == color)) {
- board.squareArray[j][i - 1].belongFlag = 1;
- }
- if ((board.squareArray[j - 1][i].belongFlag == 0) && (board.squareArray[j - 1][i].color == color)) {
- board.squareArray[j - 1][i].belongFlag = 1;
- }
- }
- else { //rest of bottom squares
- if ((board.squareArray[j][i - 1].belongFlag == 0) && (board.squareArray[j][i - 1].color == color)) {
- board.squareArray[j][i - 1].belongFlag = 1;
- }
- if ((board.squareArray[j][i + 1].belongFlag == 0) && (board.squareArray[j][i + 1].color == color)) {
- board.squareArray[j][i + 1].belongFlag = 1;
- }
- if ((board.squareArray[j - 1][i].belongFlag == 0) && (board.squareArray[j - 1][i].color == color)) {
- board.squareArray[j - 1][i].belongFlag = 1;
- }
- }
- }
- else{ //rest of squares
- if(i == 0){ //leftmost squares
- if ((board.squareArray[j-1][i].belongFlag == 0) && (board.squareArray[j-1][i].color == color)) {
- board.squareArray[j-1][i].belongFlag = 1;
- }
- if ((board.squareArray[j+1][i].belongFlag == 0) && (board.squareArray[j+1][i].color == color)) {
- board.squareArray[j+1][i].belongFlag = 1;
- }
- if ((board.squareArray[j][i+1].belongFlag == 0) && (board.squareArray[j][i+1].color == color)) {
- board.squareArray[j][i+1].belongFlag = 1;
- }
- }
- else if(i == 10){ //rightmost squares
- if ((board.squareArray[j-1][i].belongFlag == 0) && (board.squareArray[j-1][i].color == color)) {
- board.squareArray[j-1][i].belongFlag = 1;
- }
- if ((board.squareArray[j+1][i].belongFlag == 0) && (board.squareArray[j+1][i].color == color)) {
- board.squareArray[j+1][i].belongFlag = 1;
- }
- if ((board.squareArray[j][i-1].belongFlag == 0) && (board.squareArray[j][i-1].color == color)) {
- board.squareArray[j][i-1].belongFlag = 1;
- }
- }
- else{ //squares surrounded by other squares
- if ((board.squareArray[j-1][i].belongFlag == 0) && (board.squareArray[j-1][i].color == color)) {
- board.squareArray[j-1][i].belongFlag = 1;
- }
- if ((board.squareArray[j+1][i].belongFlag == 0) && (board.squareArray[j+1][i].color == color)) {
- board.squareArray[j+1][i].belongFlag = 1;
- }
- if ((board.squareArray[j][i-1].belongFlag == 0) && (board.squareArray[j][i-1].color == color)) {
- board.squareArray[j][i-1].belongFlag = 1;
- }
- if ((board.squareArray[j][i+1].belongFlag == 0) && (board.squareArray[j][i+1].color == color)) {
- board.squareArray[j][i+1].belongFlag = 1;
- }
- }
- }
- }
- i++;
- }
- i = 0;
- j++;
- }
- colorsAllFlags(color);
- }
- //colors all squares that have the flag on
- void colorsAllFlags(unsigned long color) {
- int i = 0;
- int j = 0;
- while (j < 10) {
- while (i < 10) {
- if (board.squareArray[j][i].belongFlag == 1){
- board.squareArray[j][i].color = color;
- draw_square(board.squareArray[j][i].x, board.squareArray[j][i].y, board.squareArray[j][i].size-1, color);
- }
- i++;
- }
- j++;
- i = 0;
- }
- }
- //prints a xpm
- void showXPM(char *xpm[],unsigned short x, unsigned short y ){
- int width, height;
- char *sprite, *color;
- sprite = read_xpm(xpm, &width, &height);
- unsigned short temp_x = 0;
- unsigned short temp_y = 0;
- while(temp_y < height){
- while(temp_x < width){
- color = sprite + temp_x + width * temp_y;
- set_pixel_color(temp_x + x, temp_y + y, *color);
- temp_x++;
- }
- temp_y++;
- temp_x = 0;
- }
- }
- void nrOfPlays(int plays) {
- switch (plays){
- case 1 :
- showXPM(number1, 265, 10);
- break;
- case 2:
- showXPM(number2, 265, 10);
- break;
- case 3:
- showXPM(number3, 265, 10);
- break;
- case 4:
- showXPM(number4, 265, 10);
- break;
- case 5:
- showXPM(number5, 265, 10);
- break;
- case 6:
- showXPM(number6, 265, 10);
- break;
- case 7:
- showXPM(number7, 265, 10);
- break;
- case 8:
- showXPM(number8, 265, 10);
- break;
- case 9:
- showXPM(number9, 265, 10);
- break;
- case 10:
- showXPM(number1, 265, 10);
- showXPM(number0, 313, 10);
- showXPM(number2, 385, 10);
- showXPM(number0, 433, 10);
- showXPM(bar_xpm, 361, 10);
- break;
- case 11:
- showXPM(number1, 313, 10);
- break;
- case 12:
- showXPM(number2, 313, 10);
- break;
- case 13:
- showXPM(number3, 313, 10);
- break;
- case 14:
- showXPM(number4, 313, 10);
- break;
- case 15:
- showXPM(number5, 313, 10);
- break;
- case 16:
- showXPM(number6, 313, 10);
- break;
- case 17:
- showXPM(number7, 313, 10);
- break;
- case 18:
- showXPM(number8, 313, 10);
- break;
- case 19:
- showXPM(number9, 313, 10);
- break;
- case 20:
- showXPM(number2, 265, 10);
- showXPM(number0, 313, 10);
- break;
- }
- }
- ;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement