473,583 Members | 3,386 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Is it possible to make a game like Magicsquare in Javascript?

10 New Member
I would be cool if I could develop a 3 by 3 grid that all row, column and diagonals adds up to 15, using numbers ranging from 1 to 9.
When click the "check sum button" this application should
Sum up all rows, columns and diagonals and put the result in the corresponding box
... Use if/else to check if all the results are 15, and congratulates the user if the answer is correct.
The name of text boxes should be named appropriately.
bonus (1pt) make the program check/give error message so that user will enter each number 1~9 only once.
May 28 '07 #1
13 1851
pbmods
5,821 Recognized Expert Expert
I would be cool if I could develop a 3 by 3 grid that all row, column and diagonals adds up to 15, using numbers ranging from 1 to 9.
It sure would be. How far have you gotten?
May 29 '07 #2
bergy
89 New Member
C'mon man, if you're trying to get us to do your homework at least take out lines like "bonus (1pt) make the program check..." You're not even asking a question you're asking for someone to do your work for you.

I would be cool if I could develop a 3 by 3 grid that all row, column and diagonals adds up to 15, using numbers ranging from 1 to 9.
When click the "check sum button" this application should
Sum up all rows, columns and diagonals and put the result in the corresponding box
... Use if/else to check if all the results are 15, and congratulates the user if the answer is correct.
The name of text boxes should be named appropriately.
bonus (1pt) make the program check/give error message so that user will enter each number 1~9 only once.
May 29 '07 #3
acoder
16,027 Recognized Expert Moderator MVP
C'mon man, if you're trying to get us to do your homework at least take out lines like "bonus (1pt) make the program check..." You're not even asking a question you're asking for someone to do your work for you.
You're absolutely right. That calls for my moderator hat...

The experts on this site are more than happy to help you with your problems but they cannot do your assignment/program for you. Attempt the assignment/program yourself first and post questions regarding any difficulties you have or about a particular function of the code that you don't know how to achieve.

Please read the Posting Guidelines and particularly the Coursework Posting Guidlines.

Then when you are ready post a new question in this thread.

MODERATOR
May 29 '07 #4
birdboy272
10 New Member
Magic Square in Javascript?

How would it be possible to make a game like magic square in JavaScript??

http://en.wikipedia.org/wiki/Magic_square

heres what I want it to look like I just don't know how to work the function.


<form name="zform">



<table width="200" border="1">
<tr>
<td bgcolor="#99999 9">&nbsp;</td>
<td bgcolor="#99999 9">&nbsp;</td>

<td bgcolor="#99999 9">&nbsp;</td>
<td bgcolor="#99999 9">&nbsp;</td>
<td bgcolor="#99999 9">&nbsp;</td>
</tr>
<tr>
<td bgcolor="#99999 9">&nbsp;</td>
<td><input name="box1" type="text" value="" size="3" maxlength="2" tabindex="1" /></td>
<td><input name="box2" type="text" value="" size="3" maxlength="2" tabindex="2"/></td>
<td><input name="box3" type="text" value="" size="3" maxlength="2" tabindex="3" /></td>

<td bgcolor="#CCFFF F"><input type="text" name="box4" size="3" maxlength="2" readonly></td>
</tr>
<tr>
<td bgcolor="#99999 9">&nbsp;</td>
<td><input name="5" type="text" value="" size="3" maxlength="2" tabindex="4"/></td>
<td><input name="box6" type="text" value="" size="3" maxlength="2" tabindex="5"/></td>
<td><input name="box7" type="text" value="" size="3" maxlength="2" tabindex="6"/></td>
<td bgcolor="#CCFFF F"><input type="text" name="8" size="3" maxlength="2" readonly/></td>
</tr>

<tr>
<td bgcolor="#99999 9">&nbsp;</td>
<td><input name="box9" type="text" value="" size="3" maxlength="2" tabindex="7"/></td>
<td><input name="box10" type="text" value="" size="3" maxlength="2" tabindex="8"/></td>
<td><input name="box11" type="text" value="" size="3" maxlength="2" tabindex="9"/></td>
<td bgcolor="#CCFFF F"><input type="text" name="box12" size="3" maxlength="2" readonly /></td>
</tr>
<tr>
<td bgcolor="#CCFFF F"><input type="text" name="box13" size="3" maxlength="2" tabindex="1" readonly/></td>

<td bgcolor="#CCFFF F"><input type="text" name="box14" size="3" maxlength="2" readonly/></td>
<td bgcolor="#CCFFF F"><input type="text" name="box15" size="3" maxlength="2" readonly/></td>
<td bgcolor="#CCFFF F"><input type="text" name="box16" size="3" maxlength="2" readonly /></td>
<td bgcolor="#CCFFF F"><input type="text" name="box17" size="3" maxlength="2" readonly/></td>
</tr>
</table>
<input type="button" value="check sum">

</form>
May 29 '07 #5
mrhoo
428 Contributor
<input name="box7" type="text" value="" size="3" maxlength="2" tabindex="6"/>
<input type="button" value="check sum">
Close all of your input tags or none of them-
it looks like you don't know your own mind.
May 29 '07 #6
iam_clint
1,208 Recognized Expert Top Contributor
me? i would do something like this
Expand|Select|Wrap|Line Numbers
  1. <script>
  2. function checksum() {
  3. var v1 = 0;
  4. var v2 = 0;
  5. var v3 = 0;
  6. var all = ["box1", "box2", "box3", "box5", "box6", "box7", "box9", "box10", "box11"];
  7. for (i=0; i<all.length; i++) {
  8.    for (b=0; b<all.length; b++) {
  9.     if (i!=b && document.getElementsByName(all[b])[0].value == document.getElementsByName(all[i])[0].value) { alert("Two of your numbers are the same! can't continue!");  return false; }
  10.    }
  11. }
  12. v1 = parseFloat(document.getElementsByName("box1")[0].value);
  13. v2 = parseFloat(document.getElementsByName("box2")[0].value);
  14. v3 = parseFloat(document.getElementsByName("box3")[0].value);
  15. document.getElementsByName("box4")[0].value = Math.floor(v1+v2+v3);
  16. var a1 = Math.floor(v1+v2+v3);
  17. v1 = parseFloat(document.getElementsByName("box5")[0].value);
  18. v2 = parseFloat(document.getElementsByName("box6")[0].value);
  19. v3 = parseFloat(document.getElementsByName("box7")[0].value);
  20. document.getElementsByName("box8")[0].value = Math.floor(v1+v2+v3);
  21. var a2 = Math.floor(v1+v2+v3);
  22. v1 = parseFloat(document.getElementsByName("box9")[0].value);
  23. v2 = parseFloat(document.getElementsByName("box10")[0].value);
  24. v3 = parseFloat(document.getElementsByName("box11")[0].value);
  25. document.getElementsByName("box12")[0].value = Math.floor(v1+v2+v3);
  26. var a3 = Math.floor(v1+v2+v3);
  27. v1 = parseFloat(document.getElementsByName("box3")[0].value);
  28. v2 = parseFloat(document.getElementsByName("box6")[0].value);
  29. v3 = parseFloat(document.getElementsByName("box9")[0].value);
  30. document.getElementsByName("box13")[0].value = Math.floor(v1+v2+v3);
  31. var a4 = Math.floor(v1+v2+v3);
  32. v1 = parseFloat(document.getElementsByName("box1")[0].value);
  33. v2 = parseFloat(document.getElementsByName("box5")[0].value);
  34. v3 = parseFloat(document.getElementsByName("box9")[0].value);
  35. document.getElementsByName("box14")[0].value = Math.floor(v1+v2+v3);
  36. var a5 = Math.floor(v1+v2+v3);
  37. v1 = parseFloat(document.getElementsByName("box2")[0].value);
  38. v2 = parseFloat(document.getElementsByName("box6")[0].value);
  39. v3 = parseFloat(document.getElementsByName("box10")[0].value);
  40. document.getElementsByName("box15")[0].value = Math.floor(v1+v2+v3);
  41. var a6 = Math.floor(v1+v2+v3);
  42. v1 = parseFloat(document.getElementsByName("box3")[0].value);
  43. v2 = parseFloat(document.getElementsByName("box7")[0].value);
  44. v3 = parseFloat(document.getElementsByName("box11")[0].value);
  45. document.getElementsByName("box16")[0].value = Math.floor(v1+v2+v3);
  46. var a7 = Math.floor(v1+v2+v3);
  47. v1 = parseFloat(document.getElementsByName("box1")[0].value);
  48. v2 = parseFloat(document.getElementsByName("box6")[0].value);
  49. v3 = parseFloat(document.getElementsByName("box11")[0].value);
  50. document.getElementsByName("box17")[0].value = Math.floor(v1+v2+v3);
  51. var a8 = Math.floor(v1+v2+v3);
  52. if (a1==a2 && a1==a3 && a1==a4 && a1==a5 && a1==a6 && a1==a7 && a1==a8) { alert("you win!"); } else { alert("try again"); }
  53. }
  54. </script>
  55.  
  56. <form name="zform">
  57. <table width="200" border="1">
  58. <tr>
  59. <td bgcolor="#999999">&nbsp;</td>
  60. <td bgcolor="#999999">&nbsp;</td>
  61.  
  62. <td bgcolor="#999999">&nbsp;</td>
  63. <td bgcolor="#999999">&nbsp;</td>
  64. <td bgcolor="#999999">&nbsp;</td>
  65. </tr>
  66. <tr>
  67. <td bgcolor="#999999">&nbsp;</td>
  68. <td><input name="box1" type="text" value="" size="3" maxlength="2" tabindex="1" /></td>
  69. <td><input name="box2" type="text" value="" size="3" maxlength="2" tabindex="2"/></td>
  70. <td><input name="box3" type="text" value="" size="3" maxlength="2" tabindex="3" /></td>
  71.  
  72. <td bgcolor="#CCFFFF"><input type="text" name="box4" size="3" maxlength="2" readonly></td>
  73. </tr>
  74. <tr>
  75. <td bgcolor="#999999">&nbsp;</td>
  76. <td><input name="box5" type="text" value="" size="3" maxlength="2" tabindex="4"/></td>
  77. <td><input name="box6" type="text" value="" size="3" maxlength="2" tabindex="5"/></td>
  78. <td><input name="box7" type="text" value="" size="3" maxlength="2" tabindex="6"/></td>
  79. <td bgcolor="#CCFFFF"><input type="text" name="box8" size="3" maxlength="2" readonly/></td>
  80. </tr>
  81.  
  82. <tr>
  83. <td bgcolor="#999999">&nbsp;</td>
  84. <td><input name="box9" type="text" value="" size="3" maxlength="2" tabindex="7"/></td>
  85. <td><input name="box10" type="text" value="" size="3" maxlength="2" tabindex="8"/></td>
  86. <td><input name="box11" type="text" value="" size="3" maxlength="2" tabindex="9"/></td>
  87. <td bgcolor="#CCFFFF"><input type="text" name="box12" size="3" maxlength="2" readonly /></td>
  88. </tr>
  89. <tr>
  90. <td bgcolor="#CCFFFF"><input type="text" name="box13" size="3" maxlength="2" tabindex="1" readonly/></td>
  91.  
  92. <td bgcolor="#CCFFFF"><input type="text" name="box14" size="3" maxlength="2" readonly/></td>
  93. <td bgcolor="#CCFFFF"><input type="text" name="box15" size="3" maxlength="2" readonly/></td>
  94. <td bgcolor="#CCFFFF"><input type="text" name="box16" size="3" maxlength="2" readonly /></td>
  95. <td bgcolor="#CCFFFF"><input type="text" name="box17" size="3" maxlength="2" readonly/></td>
  96. </tr>
  97. </table>
  98. <input type="button" value="check sum" onclick="checksum();">
  99. </form>
  100.  
May 29 '07 #7
acoder
16,027 Recognized Expert Moderator MVP
Since this is basically the same problem, I've merged the threads.
May 30 '07 #8
iam_clint
1,208 Recognized Expert Top Contributor
yay homework thread I didn't see the other post
May 30 '07 #9
birdboy272
10 New Member
I need a if and else that says congratulations to the person is they get 15 in all the output squares for the game magic square

this is my code so far:

<h3>
Try to make 15 in each of the green boxes without using numbers 1-9 more than once.
I will know if your cheating :-{
</h3>
<form name="zform">

<table width="200" border="1">
<tr>
<td bgcolor="#99999 9">&nbsp;</td>
<td bgcolor="#99999 9">&nbsp;</td>

<td bgcolor="#99999 9">&nbsp;</td>
<td bgcolor="#99999 9">&nbsp;</td>
<td bgcolor="#99999 9">&nbsp;</td>
</tr>
<tr>
<td bgcolor="#99999 9">&nbsp;</td>
<td><input name="box1" type="text" value="" size="3" maxlength="2" tabindex="1" /></td>
<td><input name="box2" type="text" value="" size="3" maxlength="2" tabindex="2"/></td>
<td><input name="box3" type="text" value="" size="3" maxlength="2" tabindex="3" /></td>

<td bgcolor="#CCFFF F"><input type="text" name="box4" size="3" maxlength="2" readonly></td>
</tr>
<tr>
<td bgcolor="#99999 9">&nbsp;</td>
<td><input name="box5" type="text" value="" size="3" maxlength="2" tabindex="4"/></td>
<td><input name="box6" type="text" value="" size="3" maxlength="2" tabindex="5"/></td>
<td><input name="box7" type="text" value="" size="3" maxlength="2" tabindex="6"/></td>
<td bgcolor="#CCFFF F"><input type="text" name="box8" size="3" maxlength="2" readonly/></td>
</tr>

<tr>
<td bgcolor="#99999 9">&nbsp;</td>
<td><input name="box9" type="text" value="" size="3" maxlength="2" tabindex="7"/></td>
<td><input name="box10" type="text" value="" size="3" maxlength="2" tabindex="8"/></td>
<td><input name="box11" type="text" value="" size="3" maxlength="2" tabindex="9"/></td>
<td bgcolor="#CCFFF F"><input type="text" name="box12" size="3" maxlength="2" readonly /></td>
</tr>
<tr>
<td bgcolor="#CCFFF F"><input type="text" name="box13" size="3" maxlength="2" tabindex="1" readonly/></td>

<td bgcolor="#CCFFF F"><input type="text" name="box14" size="3" maxlength="2" readonly/></td>
<td bgcolor="#CCFFF F"><input type="text" name="box15" size="3" maxlength="2" readonly/></td>
<td bgcolor="#CCFFF F"><input type="text" name="box16" size="3" maxlength="2" readonly /></td>
<td bgcolor="#CCFFF F"><input type="text" name="box17" size="3" maxlength="2" readonly/></td>
</tr>
</table>
<input type="button" value="check sum" onClick="checks um();">

</form>

<script>

function checksum(){
var sVal = zform.box4.valu e;
zform.box4.valu e = parseInt(zform. box1.value) + parseInt(zform. box2.value) + parseInt(zform. box3.value);
// line break
zform.box8.valu e = parseInt(zform. box5.value) + parseInt(zform. box6.value) + parseInt(zform. box7.value);
// line break
zform.box12.val ue = parseInt(zform. box9.value) + parseInt(zform. box10.value) + parseInt(zform. box11.value);
// line break
zform.box17.val ue = parseInt(zform. box1.value) + parseInt(zform. box6.value) + parseInt(zform. box11.value);
//line break
zform.box15.val ue = parseInt(zform. box2.value) + parseInt(zform. box6.value) + parseInt(zform. box10.value);
//line break
zform.box14.val ue = parseInt(zform. box1.value) + parseInt(zform. box5.value) + parseInt(zform. box9.value);
//line break
zform.box16.val ue = parseInt(zform. box3.value) + parseInt(zform. box7.value) + parseInt(zform. box11.value);
//line break
zform.box13.val ue = parseInt(zform. box3.value) + parseInt(zform. box6.value) + parseInt(zform. box9.value);
}

</script>
May 31 '07 #10

Sign in to post your reply or Sign up for a free account.

Similar topics

2
2205
by: Tim Simmons | last post by:
I am stumped. I encoded the action = of my form using GET and I can't seem to get the property/value stuff from it using a JavaScript script I got from the web. I want to create a trivia game where the user gets 1 question at a time and it keeps scoring until the end and gives a summary and I want to do it only in JavaScript (no ASP, PHP,...
6
1626
by: jar13861 | last post by:
Create a game in a 3x3 HTML table that works as follows: * The goal of the game is to beat the computer by scoring more points than the computer. * The game starts when a hidden random number between 1 and 9 is placed in each cell. * The user and computer take turns by "clicking" on table cells. When a cell is "clicked" by the user, the...
17
2268
by: stubbsie | last post by:
Hi, I have redesigned our official public government website in .net and it has taken me a few months to redo. I have been the sole designer of the website from its humble beginnning a few years ago when no one wanted to even mess with it. Since then I have been steadily maintaining it and improving it. I am about halfway done, and all of a...
7
2457
by: Norman Swartz | last post by:
I want to place some graphic images on the web that are optimally viewed at a resolution of 1024 by 768 pixels. Is it possible, within Javascript,to force a particular screen resolution?
0
2334
by: Bree | last post by:
The game requires it not to accept negative numbers. At the moment it isnt, and it is urgent I find the solution asap. So if anyone can help I would much appreciate it. Thanks Bree This is the code so far. <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head>
20
20353
by: NeedJavaHelp | last post by:
Hello everyone, first time poster here, bear with me. RuneScape is an online multiplayer game run by Java www.runescape.com. The game itself is run on the website and when you play the game for the first time, its files store on your pc. I've never had a problem untill its makers switched to Java Se (SUN Java). Normally, when you move to a...
2
1433
tpgames
by: tpgames | last post by:
I have not been able to find source code for a kids (2x2) sudoku game, and was wondering if anyone had the time to volunteer to make a JavaScript version for me that uses images. I only ask, because comparing the 2x3 and the 3x3 source codes I do have, does not help me figure out how to make a 2x2 sudoku game. I also ask because my 4 yr old nephew...
3
1746
by: knightsean0 | last post by:
I made a C# program (I'm fairly new to the language) with links and useful tools for an online game made in Java. However, this game crashes often (the error window with the error report thing shows that it's from msjava.dll). My question is, is it possible to make the msjava.dll problem not bring down my application with it? -Sean
11
4826
tpgames
by: tpgames | last post by:
I've struck zero in finding a Link to a sudoku game that actually uses images for numbers, even images OF numbers. Yes, I'd prefer the game in JavaScript. Every site I've been too, (hundreds) uses CSS and HTML and NOT images. I've only found 1 site that uses images, and their source code is NOT available for viewing, at all. I can't even find that...
0
7821
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
0
8172
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
0
8320
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
1
7929
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
0
6577
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
1
5697
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
0
5370
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
0
3814
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
0
3841
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.