Ta strona używa ciasteczek (cookies), dzięki którym nasz serwis może działać lepiej. Dowiedz się więcej OK, rozumiem

gra minefield - java script kod

Tworzenie dynamicznych stron internetowych z zastosowaniem JavaScript, jQuery, Ajax. Dowiedz się jak to zrobić, znajdź rozwiązania swoich problemów.

gra minefield - java script kod

Post 19.01.2017, 18:39:58

Witam,
Od niedawna zacząłem kurs internetowy java script i od kilku dni próbuję znaleźć rozwiązanie do jednego z zadanych kodów,
w którym realizatorzy kodu celowo zrobili sporo błędów a moim zadaniem jest poprawienie wszystkich aby gra działała.
Udało mi się jedynie wykryć kilka literówek i źle zdeklarowanych var jednak nie mogę rozgryźć reszty(dźwięk nie działa i nie pokazują się obrazki ). Poniżej zamieszczam 3 kody do gry.
Proszę o pomoc albo jakieś wskazówki.
Kod: Zaznacz cały
kod  java script

var grid = [];

for (i = 0; i < 49; i++)
{
grid[i]="clear";// set grid array to clear.
}

var number_of_mines = 10;
var r = 0;

while (r < number_of_mines)
{
var mine = Math.floor(Math.random() * 49);

if (grid[mine] === "clear")
{
grid[mine]="mine";
r++;
}
}

var number_of_guesses = 0;
var maximum_guesses = 45;
var number_of_hits = 0;
var number_of_misses = 0;

var element1 = opener.document.getElementById("easy");
if (element1.checked === true)
{
maximum_guesses = 45;
}
var element2 = opener.document.getElementById("intermediate");
if (element2.checked === true)
{
maximum_guesses = 40;
}
var element3 = opener.document.getElementById("advanced");
if (element3.checked === true)
{
maximum_guesses = 35;
}
var element4 = opener.document.getElementById("sound_on");
if (element4.checked === true)
{
var sound=1;
}
var element5 = opener.document.getElementById("sound_off");
if(element5.checked ===true)
{
sound=0;
}
function checkGrid()
{

var input = document.getElementById("guessInput");
var guess = input.value;
var firstChar = guess.charAt(0);
var secondChar = guess.charAt(1);

if (isNaN(firstChar) || isNaN(secondChar) ||  guess > 48)
{
window.alert("You must enter a number between 0 and 48.");
input.value = "";
input.focus();
return;
}

number_of_guesses++;
if(number_of_guesses >=maximum_guesses)
{
window.alert("Sorry,you have run out of guesses!");
return;
}

var element6 = document.getElementById("numberGuesses");
element6.value = number_of_guesses;

var cell = document.getElementById("guessInput");

if(grid[guess] == "mine")
{
cell.setAttribute("class", "hit");
number_of_hits++;
var element7 = document.getElementById("numberHits");
element7.value = number_of_hits;
if(sound === 1)
function playAudio()  {hit_sound.play();}
}
else
{
cell.setAttribute("class","miss");
number_of_misses++;
if(sound === 1)
{miss_sound.play();}
}
var input = document.getElementById("guess");
input.value = "";
input.focus();

if(number_of_hits == number_of_mines)
{
if(sound == 1)
function playAudio() {Applause.play();}
window.alert("Congratulations, you have cleared the minefield!");
return;
}

}

kod html
<!DOCTYPE html>
<html>
<head>
<title> Minefield </title>
<script type="text/javascript" src="Minefield.js">
</script>
<style>
body {
   background-color: rgb(10,100,120);
}

div#grid {
   position: relative;
   width: 700px;
   height: 730px;
   margin-left: auto;
   margin-right: auto;
   background-image: url("Minefield.jpg");
   background-repeat: no-repeat;
}

.hit {
   background: url("hit.png") no-repeat center center;
}
.miss {
   background: url("miss.png") no-repeat center center;
}

table {
   border-spacing: 0px;
   position: absolute;
   left: 0px;
   top: 0px;
}

td {
   border: 1px solid white;
   width: 98px;
   height: 100px;
   text-align: left;
   vertical-align: top;
   color: white;
   font-family: Verdana, Arial;
   font-size: 20px;
}

form {
   position: absolute;
   bottom: 0px;
   left: 0px;
   width: 690px;
   padding: 5px;
   background-color: white;
   border-color: black;
}

form input {
   background-color: rgb(0,100,115);
   border-color: black;
   font-family: Verdana, Arial;   
   font-size: 1em;
   color: white;
}
</style>

</head>
<body>
<div id="grid">
<table>
<tr>
<td id="0">0</td> <td id="1">1</td> <td id="2">2</td> <td id="3">3</td>
<td id="4">4</td> <td id="5">5</td> <td id="6">6</td>
</tr>
<tr>
<td id="7">7</td> <td id="8">8</td> <td id="9">9</td> <td id="10">10</td>
<td id="11">11</td> <td id="12">12</td> <td id="13">13</td>
</tr>
<tr>
<td id="14">14</td> <td id="15">15</td> <td id="16">16</td> <td id="17">17</td>
<td id="18">18</td> <td id="19">19</td> <td id="20">20</td>
</tr>
<tr>
<td id="21">21</td> <td id="22">22</td> <td id="23">23</td> <td id="24">24</td>
<td id="25">25</td> <td id="26">26</td> <td id="27">27</td>
</tr>
<tr>
<td id="28">28</td> <td id="29">29</td> <td id="30">30</td> <td id="31">31</td>
<td id="32">32</td> <td id="33">33</td> <td id="34">34</td>
</tr>
<tr>
<td id="35">35</td> <td id="36">36</td> <td id="37">37</td> <td id="38">38</td>
<td id="39">39</td> <td id="40">40</td> <td id="41">41</td>
</tr>
<tr>
<td id="42">42</td> <td id="43">43</td> <td id="44">44</td> <td id="45">45</td>
<td id="46">46</td> <td id="47">47</td> <td id="48">48</td>
</tr>
</table> <p> <br>
<form>
Number of guesses: <input type="text" id="numberGuesses" size="3" placeholder="0">
Number of hits: <input type="text" id="numberHits" size="3" placeholder="0">
Enter a number between 0-48: <input type="text" id="guessInput" size="3" placeholder="0">
<input type="button" id="fireButton" value="Fire!" onClick="checkGrid()">
</form>
<audio id="hit_sound">
<source src="depth_charge.mp3" type="audio/mpeg">
</audio>
<audio id="miss_sound">
<source src="beep.mp3" type="audio/mpeg">
</audio>
<audio id="Applause">
<source src="Applause.mp3" type="audio/mpeg">
</audio>
</div>
</body>
</html>

kod html do strony startowej
<!DOCTYPE html>
<html>
<head>
<title> Minefield Start Screen </title>
<script type="text/javascript">

function gameStart()
{
var level = 0;
var element1 = document.getElementById("easy");
if (element1.checked === true) {
level = 1;
}
var element2 = document.getElementById("intermediate");
if (element2.checked === true) {
level = 2;
}
var element3 = document.getElementById("advanced");
if (element3.checked === true) {
level = 3;
}

var sound = 0;
var element4 = document.getElementById("sound_on");
if (element4.checked === true) {
sound = 1;
}
var element5 = document.getElementById("sound_off");
if (element5.checked === true) {
sound = 2;
}
newWindow = window.open("Minefield.htm", "newWin");
}

</script>

<style>
body {
   background-color: rgb(10,100,120);
}

div#grid {
   position: relative;
   width: 700px;
   height: 730px;
   margin-left: auto;
   margin-right: auto;
   background-image: url("Minefield.jpg");
   background-repeat: no-repeat;
   font-family: verdana, arial;
   color: white;
}

form {
   position: relative;
   padding: 10px;
   color: white;
}
form input {
   background-color: rgb(0,100,115);
   border-color: white;
   font-family: Verdana, Arial;   
   font-size: 1em;
   color: white;
}
</style>
</head>
<body>
<div id="grid">
<center>
<h1> Welcome To The Minefield Game! </h1>
</center>
<h3> The rules of the game are simple. The minefield is represented by a 7 by 7 grid of 49 squares.
 Ten of the squares are randomly selected to contain a live mine and your aim is to detect them
within a defined number of guesses, depending on whether you select the easy, intermediate or advanced
level. You can also choose whether or not to include sound effects. Make your selections below and click
the start button. Enjoy! </h3> <p>
<h4>
<form name="init">
Select Difficulty Rating: Easy:
<input type="radio" name="difficulty" value="easy" id="easy" checked>
Intermediate:
<input type="radio" name="difficulty" value="intermediate" id="intermediate">
Advanced:
<input type="radio" name="difficulty" value="advanced" id="advanced">
<p>
Sound Effects: On:
<input type="radio" name="sound" value="on" id="sound_on" checked>
Off:
<input type="radio" name="sound" value="off" id="sound_off">
<p>
<input type="button" name="start" value="Start" onClick="gameStart()">
</form> </h4>
</div>
</body>
</html>

 
mariuszysko76
Posty: 1
Dołączył(a): 19.01.2017

Powrót do JavaScript


 


  • Podobne wątki
    Odpowiedzi
    Wyświetlone
    Ostatni post

Kto przegląda forum

Użytkownicy przeglądający ten dział: Google Adsense [Bot] i 9 gości

Hosting, Domeny, SSL

Subskrypcja

Mamy 50692 zarejestrowanych użytkowników.
Najnowszy użytkownik: 4b.ex


Nasi użytkownicy napisali:

  • 936086 wiadomości
  • w 246725 tematach

Najnowsze wpisy na blogu

Najnowsze artykuły

Najaktywniejsi (ostatnie 30 dni)