Hey guys,
gonna need some help with this sudoku coding.
simply doing for quite sometimes but it doesnt seems working in my html.

im using the x & y random number.

any comment on this ?

Dani AI

Generated

asked for the code and posted a working-but-fragile prototype. The main problems are not the random numbers or the timer by themselves, but three common mistakes visible in that code: relying on "sum == 10" to validate a row (that allows duplicates), naming collisions between form controls (e.g. a timer field and a cell sharing the same name/id), and using the browser form reset which reverts to the original HTML value attributes (so JS-generated clues disappear). The fixes below are minimal and practical.

A robust 4x4 validation strategy: read the 4x4 values into a 2D array, then check each row, each column, and each 2x2 block for two things: every entry is 1..4 and there are no duplicates. A quick JS pattern uses Sets to test uniqueness:

function readBoard() {
  var board = [[],[],[],[]];
  document.querySelectorAll('input[data-r][data-c]').forEach(function(inp){
    board[+inp.dataset.r][+inp.dataset.c] = parseInt(inp.value,10) || 0;
  });
  return board;
}

function allUnique(values){
  return values.every(v => v>=1 && v<=4) && new Set(values).size === 4;
}

On timeout use a single timer with clearInterval, then notify and disable inputs (avoid mixing string calls to setTimeout). Example pattern:

var timer = setInterval(tick, 1000);
function tick(){ seconds--; if(seconds<=0){ clearInterval(timer); alert('Time up'); disableAll(); } }

Keep generated clues across resets by setting the input defaultValue when placing a clue, or avoid form.reset entirely and implement a custom reset that only clears non-clue cells. Example:

inp.value = clue;
inp.defaultValue = String(clue); // preserves clue if form.reset() is called
inp.disabled = true;

Practical tips: use Math.floor(Math.random()*4)+1 for 1..4, rename the timer field so it does not conflict with a cell id, use event listeners instead of inline handlers, and consult MDN refs for Math.random, setInterval, and HTMLFormElement.reset for correct behavior (Math.random, setInterval, form.reset/defaultValue).

Recommended Answers

All 3 Replies

where's the code

<html>
<head>
<title> Sudoku game </title>

<script type="text/javascript">

a4 = Math.floor(Math.random()*5); 
while (a4==0)
{
a4 = Math.floor(Math.random()*5); 
}

c2 = Math.floor(Math.random()*5); 
while (c2==0)
{
c2 = Math.floor(Math.random()*5); 
}

</script>
</head>

<body>
<center>

<h1> Sudoku game </h1>

<form name="counter"><input type="text" size="8" 
name="d2"></form> 

<script> 
 var msec=0 
 var sec=90

function display()
{ 
 if (msec<=0)
	{ 
    msec=9 
    sec-=1 
	} 
 if (sec<=-1)
	{ 
    msec=0 
    sec+=1 
	} 
 else 
    msec-=1 
    document.counter.d2.value=sec+ "." +msec 
    setTimeout("display()",90) 
} 
display() 

</script> 

<form>
<table>

<tr>
<td><input type="text" id="a1" size="3"  /></td>
<td><input type="text" id="a2" size="3" /></td>
<td><input type="text" id="a3" size="3" /></td>
<td><input type="text" id="a4" size="3" /></td>
</tr>
</td>
<tr>
<td><input type="text" id="b1" size="3" /></td>
<td><input type="text" id="b2" size="3" /></td>
<td><input type="text" id="b3" size="3" /></td>
<td><input type="text" id="b4" size="3" /></td>
</tr>
<tr>
<td><input type="text" id="c1" size="3" /></td>
<td><input type="text" id="c2" size="3" /></td>
<td><input type="text" id="c3" size="3" /></td>
<td><input type="text" id="c4" size="3" /></td>
</tr>
<tr>
<td><input type="text" id="d1" size="3" /></td>
<td><input type="text" id="d2" size="3" /></td>
<td><input type="text" id="d3" size="3" /></td>
<td><input type="text" id="d4" size="3" /></td>
</tr>

</table>
<br/><br/>

<input type="button" value="Submit" onClick="submit();"/>
<input type="button" value="Refresh" onclick="reset()" />


</form>
</center>

<script type="text/javascript">

document.getElementById("a4").value=a4;
document.getElementById("a4").disabled="disabled";
window.status=a4;

document.getElementById("c2").value=c2;
document.getElementById("c2").disabled="disabled";
window.status=c2;

</script>

</body>
</html>

i think i'll random 2numbers.

Problems Im currently facing:
1) How to set counter that mark 1st , 2nd , 3rd & 4th row that form
1+2+3+4 , 2+3+4+1 , 3+4+1+2,....... total sum up to "10"

2) Prompt when the timer end.

3) Reset button what will restart the game. Once reset , my 2 random number will disappear by itself.

Thanks you people out there.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.