HiveBrain v1.2.0
Get Started
← Back to all entries
patternMinor

A Sudoku game made from Google's Dart language

Submitted by: @import:stackexchange-codereview··
0
Viewed 0 times
madegooglelanguagegamefromsudokudart

Problem

This is my first real web project and I have never touched JavaScript (barely touched CSS), so I just skipped that and went to dart for fun. Here's a live demo

The code for the dart file is down below. If you could help me improve the code in any way, that would be nice.

Sudoku.dart

```
import 'dart:html';

TableElement board;
var tbody;
var tableCell;
var currentRow = 0;
var currentCell = 0;
String puzzle = "200000060000075030048090100000300000300010009000008000001020570080730000090000004";
int counter = 0;

void main() {
board = new TableElement();
board..setAttribute("border", "1");
tbody = board.createTBody();
makeSudokuBoard();
ChangeCurrentCell();
window.onKeyDown.listen(mykeyDown);
window.onKeyUp.listen(callCheckBoard);
window.onClick.listen(clickDown);

}

void clickDown(Event e){
CheckCurrentCell();
ChangeCurrentCell();
CheckBoard();
}

void mykeyDown(Event e){
if(e is KeyboardEvent){
KeyboardEvent kevent = e;

switch(e.keyCode){

case 38:
currentRow--;
while(!CheckNextCell())currentRow--;

ChangeCurrentCell();
CheckBoard();
break;
case 40:
currentRow++;
while(!CheckNextCell())currentRow++;

ChangeCurrentCell();
CheckBoard();
break;
case 37:
currentCell--;
while(!CheckNextCell())currentCell--;
ChangeCurrentCell();
CheckBoard();
break;
case 39:
currentCell++;
while(!CheckNextCell())currentCell++;

ChangeCurrentCell();
CheckBoard();
break;

default:
CheckCurrentCell();
ChangeCurrentCell();
CheckBoard();
break;

}
}
}

void callCheckBoard(Event e){
CheckBoard();
}

void CheckBoard(){
var check = 0;
TableRowElement r = board.rows[currentRow];
bool worked = true;
List c = r.cells;
var current = tableCell.text;
try{
var foo = int.parse(current);
}catch(e){
worked = false;

Solution

First, try running the code through the Dart Formatter.

Second, try putting all of the game logic into one or more classes. Most CS professors – and development professionals – hate to see top-level, mutable state.

Finally, it's good to always be reading more code than you're writing. You'll learn a lot. For a game like this you might want to look at Pop, Pop, Win!. It's a good example of separating game state from the game visualization.

If you're using the Dart Editor, you can open the sample by clicking on the link on the Welcome Page.

Keep up the hacking!

Context

StackExchange Code Review Q#51778, answer score: 5

Revisions (0)

No revisions yet.