java数独游戏_java 数独sudoku游戏

有大佬帮帮看下这个数独游戏程序么,检测为空、有效都成功了,就是不能把赋值更新到数组里面,一直重复打印原数组

package pro1;

import java.util.Scanner;

public class Sudoku_yl {

// public void printFrame(){

// // we got the answer, just print it

// System.out.print("------|------|------|");

// }

//

//public void printGrid(int[][] grid) {

// for(int i = 0; i < grid.length;i++) {

// for(int j = 0; j < grid[0].length;j++) {

// System.out.print(gridi + " ");

// if((j+1) % 3 == 0) {

// System.out.print("| ");

// }

// }

// while((i+1) % 3 == 0) {

// System.out.println();

// printFrame();

// break;

// }

// System.out.println();

// }

//}

public void printFrame(){

System.out.print("------|-------|-------|");

}

public void printGrid(int[][] grid){

for(int i=0;i

for(int j=0;j

System.out.print(grid[i][j]+" ");

if((j+1)%3==0)

System.out.print("| ");

}

while((i+1)%3==0){

System.out.println();

printFrame();

break;

}

System.out.println();

}

}

@SuppressWarnings("resource")

public void Play(int[][] grid) {

// 一,分析将要填的格子的数量;二,循环玩这个游戏;三,打印;四,判断有效

// Analyze the number of cells need to fill

int remain = 0;

for(int i = 0; i < grid.length; i++){

for(int j = 0;j < grid.length; j++){

if(grid[i][j] == 0) {

remain++;

}

}

}

// Play the game until the end of the game

while(remain>0)

{

System.out.print("Enter row, column and number [1-9] [1-9] [1-9]:");

Scanner input = new Scanner(System.in);

int r = input.nextInt();

int c = input.nextInt();

int n = input.nextInt();

if(grid[r-1][c-1] != 0 ) {

System.out.println("The cell is not empty!");

}

else {

if(isValid(r, c, n, grid)){

grid[r][c] = n;

System.out.println();

System.out.println("The current state of grid:");

System.out.println();

printGrid(grid);

printFrame();

System.out.println();

// printGrid(grid);

remain--;

}

else {

System.out.println("The number is irrational.");

}

}

}

}

public static boolean isValid(int r, int c, int n, int[][] grid) {

// check whether n is valid at the r-1 row

for(int d = 0; d < grid.length;d++) {

//if the number we are trying to place is already present in

// that r, return false;

if(grid[r][d] == n) {

return false;

}

}

// check whether n is valid at the c-1 column

for(int q = 0; q < grid.length;q++) {

//if the number we are trying to place is already present in that column, return false;

if(grid[q][c] == n) {

return false;

}

}

//check whether n is valid in the 3 by 3 box

int sqrt = (int)Math.sqrt(grid.length);

int boxRowStart = r - r % sqrt;

int boxColumnStart = c - c % sqrt;

for(int t = boxRowStart; t < boxRowStart + sqrt;t++) {

for(int d = boxColumnStart; d < boxColumnStart + sqrt; d++) {

if(grid[t][d] == n) {

return false;

}

}

}

return true;// The value n at grid[i][j] is valid

}

public static void main(String[] args) {

int[][] grid = {{5,3,0,0,7,0,0,0,0},

{6,0,0,1,9,5,0,0,0},

{0,9,8,0,0,0,0,6,0},

{8,0,0,0,6,0,0,0,3},

{4,0,0,8,0,3,0,0,1},

{7,0,0,0,2,0,0,0,6},

{0,6,0,0,0,0,0,0,0},

{0,0,0,4,1,9,0,0,5},

{0,0,0,0,8,0,0,7,9}};

Sudoku_yl A= new Sudoku_yl();

A.Play(grid);

A.printGrid(grid);

}

}

题目描述

题目来源及自己的思路

相关代码

// 请把代码文本粘贴到下方(请勿用图片代替代码)

你期待的结果是什么?实际看到的错误信息又是什么?

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值