Hi, I am working on the Artificial Intelligence: Teaching the Computer to Play Tic-Tac-Toe project. I got to the 6th step without any problems; however, after updating the code related to starting a new game, the buttons for the regular squares are not working. I have not changed anything from the recommended code. I think the problem is in the newgame function section since the x and o buttons work fine without that function. Thank you in advance for your time!
Here is a copy of my code:
<!DOCTYPE html>
<html>
<head>
<script type="text/javascript">
var xTurn = true;
function squareclicked(square)
// squareclicked is a function that is called whenever a button is clicked.
{
var status = document.getElementById('status');
var value = square.value;
if (value != 'X' && value != 'O')
{
if (xTurn)
{
square.value = 'X';
xTurn = false;
status.innerHTML = 'O\'s turn';
}
else
{
square.value = 'O';
xTurn = true;
status.innerHTML = 'X\'s turn';
}
}
else
alert('That square has already been played.');
}
function newgame()
//newgame is a function that resets the game when called.
{
var status = document.getElementById('status');
xTurn = true;
status.innerHTML = 'X\'s turn';
for(var x = 0; x < x++)
{
for(var y = 0; y < y++)
{
document.getElementById(x + '_' + y).value = ' ';
}
}
}
</script>
</head>
<body>
<input type="button" id="0_0" value=" " onclick="squareclicked(this);">
<input type="button" id="1_0" value=" " onclick="squareclicked(this);">
<input type="button" id="2_0" value=" " onclick="squareclicked(this);">
<br>
<input type="button" id="0_1" value=" " onclick="squareclicked(this);">
<input type="button" id="1_1" value=" " onclick="squareclicked(this);">
<input type="button" id="2_1" value=" " onclick="squareclicked(this);">
<br>
<input type="button" id="0_2" value=" " onclick="squareclicked(this);">
<input type="button" id="1_2" value=" " onclick="squareclicked(this);">
<input type="button" id="2_2" value=" " onclick="squareclicked(this);">
<br>
<input type="button" id="newgame" value="New Game" onclick="newgame();">
<P ID="status">X's turn</P>
</body>
</html>
Computer Tic Tac Toe: Button Problem
Ask questions about projects relating to: computer science or pure mathematics (such as probability, statistics, geometry, etc...).
Moderators: kgudger, bfinio, Moderators
-
- Posts: 1
- Joined: Thu Jun 15, 2023 1:10 pm
- Occupation: Student
Return to “Grades 9-12: Math and Computer Science”
Jump to
- Announcements
- Active Forums (Make all new posts here)
- ↳ Grades K-5
- ↳ Grades K-5: Life, Earth, and Social Sciences
- ↳ Grades K-5: Physical Science
- ↳ Grades K-5: Math and Computer Science
- ↳ Grades 6-8
- ↳ Grades 6-8: Life, Earth, and Social Sciences
- ↳ Grades 6-8: Physical Science
- ↳ Grades 6-8: Math and Computer Science
- ↳ Grades 9-12
- ↳ Grades 9-12: Life, Earth, and Social Sciences
- ↳ Grades 9-12: Physical Science
- ↳ Grades 9-12: Math and Computer Science
- ↳ Advanced Science Competitions (Intel ISEF, Intel STS, Siemens Competition, JSHS, etc.)
- ↳ Getting Started
- ↳ Preparing for the Competition
- ↳ STEM Activities
- ↳ Interview Requests
- ↳ Careers in Science
- ↳ Raspberry Pi Projects Kit
- ↳ Junior Solar Sprint
- ↳ SimPandemic
- ↳ Using SimPandemic
- ↳ General Discussion
- Archived
- ↳ Preparing for the Science Fair
- ↳ Intel ISEF Preparation
- ↳ Science Teachers: Fairs, Projects, and General Support
- ↳ Grades K-5: Getting Ready for the Science Fair
- ↳ Grades 6-8: Getting Ready for the Science Fair
- ↳ Grades 9-12: Getting Ready for the Science Fair
- ↳ Physical Science
- ↳ Life, Earth, and Social Sciences
- ↳ Math & Computer Science Sponsored by Hyperion Solutions Corp
- ↳ General Questions
- ↳ InnovatioNation Teachers
- ↳ Grades 6-8: Wind Tunnel
- ↳ Grades 9-12: Wind Tunnel
- ↳ A Magnetic Primer Designer
- ↳ Bridge Design
- ↳ Build a Gauss Rifle!
- ↳ Build a Simple Circuit
- ↳ Build Your Own Crystal Radio
- ↳ Chemistry of Ice-Cream Making
- ↳ Do Oranges Lose or Gain Vitamin C After Being Picked?
- ↳ Electrolyte Challenge: Orange Juice vs. Sports Drink
- ↳ Enzyme-Catalyzed Reactions
- ↳ Forensic Science: Building Your Own Tool for Identifying DNA
- ↳ From Dull to Dazzling: Using Pennies to Test How pH Affects Copper Corrosion
- ↳ How Do Roots Grow...
- ↳ How Does Soil Affect the pH of Water
- ↳ Investigating the 'Mpemba Effect': Can Hot Water Freeze Faster than Cold Water?
- ↳ Is this connected to that? Use a homemade electronic tester to find out if electricity can flow between two objects.
- ↳ How Are Antibodies Used for Blood Typing?
- ↳ It's All in the Wrist
- ↳ Measuring the Sugar Content on a Liquid with a Laser Pointer
- ↳ Potions and Lotions
- ↳ Shaking Up Some Energy
- ↳ Spin Right 'Round with this Simple Electric Motor
- ↳ Sweet as Sugar
- ↳ The Strength of an Electromagnet
- ↳ Veggie Power! Making Batteries from Fruits and Vegetables
- ↳ Which Orange Juice Has the Most Vitamin C?