繁体   English   中英

C 中的数独点击框架

[英]Sudoku in C clicking in frame

当用户在数独游戏的框框内单击时,我试图触发打印。 对于框外的点击,我可以这样做。 但我不确定出了什么问题。 感谢为我的任务提供的任何帮助!

特别是在函数 int check() 上

图形界面

#include "Sudoku.h"
#include "GUI.h"
#include <stdlib.h>
#include <stdio.h>
#include <math.h>
#include <time.h>
#include <string.h>
#include "graphics2.h"

/*You cannot change the name of these constants but you can change their               definition*/
#define MARGIN 120
#define GRId_SIZE (MAX_X - 400)/BOARD_SIZE
#define STEP ((MAX_X)/(GRId_SIZE*(BOARD_SIZE)))
#define X_START_OFFSET MARGIN
#define X_END_OFFSET (GRId_SIZE + MARGIN)
#define Y_START_OFFSET MARGIN
#define Y_END_OFFSET (MARGIN+GRId_SIZE)
#define BORDER_OFFSET GRId_SIZE/20

void drawSections(){
/*Question 1
You have to use this function in your implementation
Please ENTER YOUR OWN SOURCE CODE*/
}

int isClickOutOfBoard() {
int mouseX = mouseclickx(), mouseY = mouseclicky();
if (mouseX < X_START_OFFSET || mouseY < Y_START_OFFSET 
    || mouseX > X_START_OFFSET + GRId_SIZE * BOARD_SIZE 
    || mouseY > Y_START_OFFSET + GRId_SIZE * BOARD_SIZE) {
    printf("Out of Board\n");
    printf("X: %d Y: %d\n", mouseX, mouseY);
    return 0;
}
return 0;
}

int isClickInSelection() {
int mouseX = mouseclickx(), mouseY = mouseclicky();
if (mouseX > X_START_OFFSET && mouseX < X_START_OFFSET + GRId_SIZE * BOARD_SIZE &&
    mouseY > Y_START_OFFSET + GRId_SIZE * (BOARD_SIZE + 1) &&
    mouseY < Y_START_OFFSET + GRId_SIZE * (BOARD_SIZE + 2)){
    printf("In Selection\n");
    printf("X: %d Y: %d\n", mouseX, mouseY);
    return 0;
}
printf("Selection");
return 1;
}



int getMouseX(){
/*You can change this function*/
int mc = mouseclickx();
mc -= X_START_OFFSET;
int gz = GRId_SIZE;
mc = mc / gz;
return mc;
}

int getMouseY(){
/*You can change this function*/
int mc = mouseclicky();
mc -= Y_START_OFFSET;
int gz = GRId_SIZE;
mc = mc / gz;
return mc;
}

int getSelected() {
int mc = mouseclickx();
mc -= X_START_OFFSET;
int gz = Y_END_OFFSET + MARGIN;
mc = mc / gz;
return mc;
}

void printAtBottom(char *text){
/*You can change this function*/
settextstyle(10, HORIZ_DIR, GRId_SIZE / 14);
outtextxy(20, MAX_Y-100, text);
}

void initializeGraphics(){
/*You can change this function*/
int GraphDriver = 0, GraphMode = 0;
initgraph(&GraphDriver, &GraphMode, "", MAX_X, MAX_Y); // Start Window
cleardevice();
}

void drawHeader(){
/*You can change this function*/
setcolor(WHITE);
settextstyle(10, HORIZ_DIR, 5);
outtextxy(20, 20, "SuDOkU - ICT1002");
settextstyle(10, HORIZ_DIR, 2);
setcolor(RED);
outtextxy(20, 70, "Copyright � SIT/ICT 2015 ");
}


void drawGridCell(int x, int y, int color){
/*You can change this function*/
setcolor(color);
setlinestyle(SOLID_LINE, 0, GRId_SIZE / 20);
setfillstyle(SOLID_FILL, color);

bar(x*GRId_SIZE + X_START_OFFSET,
    y * GRId_SIZE + Y_START_OFFSET,
    x * GRId_SIZE + X_END_OFFSET,
    y * GRId_SIZE + Y_END_OFFSET);
setcolor(WHITE);
rectangle(x*GRId_SIZE + X_START_OFFSET,
    y * GRId_SIZE + Y_START_OFFSET,
    x * GRId_SIZE + X_END_OFFSET,
    y * GRId_SIZE + Y_END_OFFSET); //Outline
}

void setGridValue(int x, int y, int g){
/*You can change this function*/
int color;
switch (g) {
case EMPTY: 
    color = DARKGRAY;
    drawGridCell(x, y, color);
    break;
case 0:
    drawGridCell(x, y, LIGHTGRAY);
    break;
default:    
    color = BLACK;
    drawGridCell(x, y, color);
    settextstyle(10, HORIZ_DIR, GRId_SIZE /13);
    char value[10]="";
    _itoa(g, value, 10);
    outtextxy(x*GRId_SIZE + X_START_OFFSET+GRId_SIZE/5.5,
        y * GRId_SIZE + Y_START_OFFSET + GRId_SIZE / 5.5, value);
    break;
}
drawSections();

数独

#include "Sudoku.h"
#include "GUI.h"
#include <time.h>

int board[BOARD_SIZE][BOARD_SIZE] = { EMPTY };
int mouseX = -1, mouseY = -1;


int checkRow(int x, int y, int value) {
int row, col;

row = y;
col = 0;

for (; col < BOARD_SIZE; col++) {
    if (value == board[col][row]) {
        return FALSE;
    }
}

return TRUE;
}

int checkColumn(int x, int y, int value) {
int row, col;

row = 0;
col = x;

for (; row < BOARD_SIZE; row++) {
    if (value == board[col][row]) {
        return FALSE;
    }
}

return TRUE;

}

int checkSection(int x, int y, int value) {
int sectionX, sectionY, sectionSize;

sectionSize = sqrt(BOARD_SIZE);
sectionX = x / sectionSize;
sectionY = y / sectionSize;

for (x = sectionX * sectionSize; x < (sectionX + 1) * sectionSize; x++) {
    for (y = sectionY * sectionSize; y < (sectionY + 1) * sectionSize; y++) {
        if (value == board[x][y]) {
            return FALSE;
        }
    }
}

return TRUE;
}

int isInputValid(int x, int y, int value) {
return checkRow(x, y, value) &&
    checkColumn(x, y, value) &&
    checkSection(x, y, value);
}

void initializeGame() {
/*You have to use this function in your implementation
You can add more code to this function*/
int i, j;
for (i = 0; i < BOARD_SIZE; i++) {
    for (j = 0; j < BOARD_SIZE; j++) {
        board[i][j] = EMPTY;
        setGridValue(i, j, EMPTY);
    }
}
j = BOARD_SIZE + 1;
for (i = 1; i <= BOARD_SIZE; i++) {
    setGridValue(i - 1, j, i);
}
drawHeader();
drawSections();
}

int getSelection() {
/*You have to use this function in your implementation*/
int selection = 0;
if (isClickInSelection()) {
    selection = getMouseX();
}

return selection;
}

int check() {
int selection, x, y;
selection = getMouseX() + 1;

if (checkRow(x, y, selection) == 0) {
    if (checkColumn(x, y, selection) == 0) {
        if (checkSection(x, y, selection) == 0) {
            setGridValue(x, y, selection);
            board[x][y] = selection;
            printAtBottom("Valid selection!");
        }
        else {
            setGridValue(x, y, EMPTY);
            printAtBottom("Section conflict!");
        }
    }
}
}

void startGame() {
/*You have to use this function in your implementation
You can add more code to this function*/
int x = 0;
static int state, new = 0;

if (mouseup()) {
    if (!isClickOutOfBoard()) {
        state = 1;
        mouseX = getMouseX();
        mouseY = getMouseY();
        setGridValue(mouseX, mouseY, 0);
        printAtBottom("Select a value");
    }

else if(state == 1) {
        new = getSelection(mouseX, mouseY);
        if (new == 0) {
            return;
        }
        else {
            state = 0;
        }
    }
}
}

void main()
{
initializeGraphics();
cleardevice();
clearmouse();
initializeGame();

/*You can change code from this point*/
while (TRUE) {
    startGame();
}
getch();
}
int isClickInSelection() {
int mouseX = mouseclickx(), mouseY = mouseclicky();
if (mouseX > X_START_OFFSET && mouseX < X_START_OFFSET + GRId_SIZE * BOARD_SIZE &&
mouseY > Y_START_OFFSET + GRId_SIZE * (BOARD_SIZE + 1) &&
mouseY < Y_START_OFFSET + GRId_SIZE * (BOARD_SIZE + 2)){
printf("In Selection\n");
printf("X: %d Y: %d\n", mouseX, mouseY);
return 0;
}
printf("Selection");
return 1;
}

通过切换返回解决。 谢谢大家

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM