您如何显示用户在板上按下了移动数独游戏的位置?

问题描述

我正在用Java-Intellij做数独游戏。我想向用户显示他们按下的位置,但我不知道该怎么做。 如果您需要整个文件在github上: https://github.com/SamirSydykov03/Sudoku-Varients-for-mobile-phones

这是我的Game.java


import com.example.sudokuvarients.normalsudoku9x9.SudokuGrid.GameGrid;
public class Game {
    private static Game instance;
    private GameGrid grid = null;
    private int selectedX = -1;
    private int selectedY = -1;
    private Game(){}
    public static Game getInstance(){
        if(instance == null){
            instance = new Game();
        }
        return instance;
    }
    public void createGrid(Context context){
        int[][] Sudoku = SudokuGenerator.getInstance().generateGrid();
        Sudoku = SudokuGenerator.getInstance().removal(Sudoku);
        grid = new GameGrid(context);
        grid.setGrid(Sudoku);
    }

    public GameGrid getGrid(){
        return grid;
    }

    public void setSelectedPos(int x,int y){
        setSelectedItem();
        selectedX = x;
        selectedY = y;

    }
    public void setSelectedItem(){
        grid.getItem(-1,-1);
    }

    public void setNumber(int number){
        if(selectedY != -1 && selectedX != -1){
            grid.setItem(selectedX,selectedY,number);
        }
        grid.checkGame();
    }

这是我的BaseSudokoCell.java

package com.example.sudokuvarients.normalsudoku9x9.SudokuGrid;

import android.content.Context;
import android.view.View;

public class BaseSudokuCell extends View {

    private boolean modify = true;
    private int value;
    private boolean selected;

    public BaseSudokuCell(Context context) {
        super(context);
    }

    @Override
    protected void onMeasure(int widthMeasureSpec,int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec,widthMeasureSpec);
    }
    public void setNotModify(){
        modify = false;
    }
    public void setinitialValue(int value){
        this.value = value;
        invalidate();
    }

    public void setValue(int value){
        if(modify){
            this.value = value;
        }
        invalidate();
    }
    public boolean getSelect(){
        return selected;
    }
    public int getValue(){
        return  value;
    }
}

这是我的SudokuCell.java-它确实绘制了一个图块,但是它没有隐藏并且在所有单元格中都绘制了。单击该按钮不会执行任何操作。

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Rect;
import android.view.View;

public class SudokuCell extends BaseSudokuCell{

    private Paint mPaint;


    public SudokuCell(Context context){
        super(context);
        mPaint =new Paint();

    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        drawNumber(canvas);
        drawLines(canvas);
            drawRectangle(canvas);

    }

    private void drawLines(Canvas canvas) {
        mPaint.setColor(Color.magenta);
        mPaint.setStyle(Paint.Style.stroke);
        mPaint.setstrokeWidth(5);
        canvas.drawRect(0,getWidth(),getHeight(),mPaint);
    }

    private void drawNumber(Canvas canvas) {
        mPaint.setColor(Color.CYAN);
        mPaint.setTextSize(60);
        mPaint.setStyle(Paint.Style.FILL);
        Rect bounds = new Rect();
        mPaint.getTextBounds(String.valueOf(getValue()),String.valueOf(getValue()).length(),bounds);
        if (getValue() != 0) {
            canvas.drawText(String.valueOf(getValue()),(getWidth() - bounds.width()) / 2,(getHeight() + bounds.height()) / 2,mPaint);
        }

    }

    private void drawRectangle(Canvas canvas){
        Rect ourRect = new Rect();
        ourRect.set(0,canvas.getWidth()-5,canvas.getHeight()-5);
        Paint grey = new Paint();
        grey.setColor(Color.GRAY);
        grey.setStyle(Paint.Style.FILL);
        canvas.drawRect(ourRect,grey);

    }



}

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)