473,387 Members | 1,691 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

Is it possible to move the checkerboard pieces with the hasClass and the addClass?

74 64KB
I have this red and black checkerboard pieces on a checkerboard, but how do I get them to move. For example, if I wanted to move the first checkerboard red piece to the seventeenth cell how would I do that? To clarify, what I want to be done is when I click on the first red highlighted checkerboard piece, I want that to move to next closet unoccupied cell, which in my case would be the seventeenth cell of the checkerboard. Please any examples with any elements on the checkerboard would be much appreciated in order for me to fully learn how to move the checkerboard pieces. I have my three files which are a JavaScript file, CSS file, and HTML file.

Expand|Select|Wrap|Line Numbers
  1. <div class="home">
  2.  
  3.                 <div class="board">
  4.                     <div class="row">
  5.                         <div class="cell piece red"></div>
  6.                         <div class="cell piece red"></div>
  7.                         <div class="cell piece red"></div>
  8.                         <div class="cell piece red"></div>
  9.                         <div class="cell piece red"></div>
  10.                         <div class="cell piece red"></div>
  11.                         <div class="cell piece red"></div>
  12.                         <div class="cell piece red"></div>
  13.                     </div>
  14.                     <div class="row">
  15.                         <div class="cell piece red"></div>
  16.                         <div class="cell piece red"></div>
  17.                         <div class="cell piece red"></div>
  18.                         <div class="cell piece red"></div>
  19.                         <div class="cell piece red"></div>
  20.                         <div class="cell piece red"></div>
  21.                         <div class="cell piece red"></div>
  22.                         <div class="cell piece red"></div>
  23.                     </div>
  24.                     <div class="row">
  25.                         <div class="cell"></div>
  26.                         <div class="cell"></div>
  27.                         <div class="cell"></div>
  28.                         <div class="cell"></div>
  29.                         <div class="cell"></div>
  30.                         <div class="cell"></div>
  31.                         <div class="cell"></div>
  32.                         <div class="cell"></div>
  33.                     </div>
  34.                     <div class="row">
  35.                         <div class="cell"></div>
  36.                         <div class="cell"></div>
  37.                         <div class="cell"></div>
  38.                         <div class="cell"></div>
  39.                         <div class="cell"></div>
  40.                         <div class="cell"></div>
  41.                         <div class="cell"></div>
  42.                         <div class="cell"></div>
  43.                     </div>
  44.                     <div class="row">
  45.                         <div class="cell"></div>
  46.                         <div class="cell"></div>
  47.                         <div class="cell"></div>
  48.                         <div class="cell"></div>
  49.                         <div class="cell"></div>
  50.                         <div class="cell"></div>
  51.                         <div class="cell"></div>
  52.                         <div class="cell"></div>
  53.                     </div>
  54.                     <div class="row">
  55.                         <div class="cell"></div>
  56.                         <div class="cell"></div>
  57.                         <div class="cell"></div>
  58.                         <div class="cell"></div>
  59.                         <div class="cell"></div>
  60.                         <div class="cell"></div>
  61.                         <div class="cell"></div>
  62.                         <div class="cell"></div>
  63.                     </div>
  64.                     <div class="row">
  65.                         <div class="cell piece black"></div>
  66.                         <div class="cell piece black"></div>
  67.                         <div class="cell piece black"></div>
  68.                         <div class="cell piece black"></div>
  69.                         <div class="cell piece black"></div>
  70.                         <div class="cell piece black"></div>
  71.                         <div class="cell piece black"></div>
  72.                         <div class="cell piece black"></div>
  73.                     </div>
  74.                     <div class="row">
  75.                         <div class="cell piece black"></div>
  76.                         <div class="cell piece black"></div>
  77.                         <div class="cell piece black"></div>
  78.                         <div class="cell piece black"></div>
  79.                         <div class="cell piece black"></div>
  80.                         <div class="cell piece black"></div>
  81.                         <div class="cell piece black"></div>
  82.                         <div class="cell piece black"></div>
  83.                     </div>
  84.                 </div>
  85.  
  86.             </div>
  87.  
  88.     </body>
  89.  
  90. </html>
Expand|Select|Wrap|Line Numbers
  1. .home > .board
  2. {
  3.     display: table;
  4.     border: 1px solid black;
  5. }
  6.  
  7. .home > .board > .row
  8. {
  9.     display: table;
  10. }
  11.  
  12. .home > .board > .row > .cell
  13. {
  14.     width: 80px;
  15.     height: 80px;
  16.     border: 1px solid black;
  17.     display: inline-block;
  18. }
  19.  
  20. .piece
  21. {
  22.     background-size: 75px 75px;
  23.     background-repeat: no-repeat;
  24.     background-position: center;
  25.     background-origin: content-box;
  26. }
  27.  
  28. .red
  29. {
  30.     background-image: URL('../images/red.png');
  31. }
  32.  
  33. .black
  34. {
  35.     background-image: URL('../images/black.png');
  36. }
Expand|Select|Wrap|Line Numbers
  1. $(document).ready(function()
  2. {
  3.     var cells = $(".cell");
  4.     var colorCount = 0; 
  5.  
  6.     for (var i = 0; i < cells.length; i++)
  7.     {
  8.         var cell = $(cells[i]);
  9.         var isDark = colorCount % 2 == 0;
  10.         var isNextRow = (i + 1) % 8 == 0;
  11.         colorCount += isNextRow ? 2 : 1;
  12.         cell.css("background-color", isDark ? "navy" : "white"); 
  13.     }
  14.     $(".cell.piece.red").on({
  15.         click: function () {
  16.             $(this).css("background-color", "yellow"); }
  17.     });
  18.     $(".cell.piece.black").on({
  19.         click: function () {
  20.             $(this).css("background-color", "yellow");  }
  21.     });
  22.  
  23.  
  24. });
Feb 26 '17 #1
0 1200

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

Similar topics

36
by: rbt | last post by:
Say I have a list that has 3 letters in it: I want to print all the possible 4 digit combinations of those 3 letters: 4^3 = 64 aaaa
8
by: tranky | last post by:
i hope you can help me! It's possible create a moveable panel in vb.net? Click with mouse over it and move it everywhere!?!? It's possible? thank u!
11
by: vbgunz | last post by:
Hello all, I am just learning Python and have come across something I feel might be a bug. Please enlightenment me... The following code presents a challenge. How in the world do you provide an...
15
by: Baron Samedi | last post by:
Every so often, I see someone wanting to prevent heir images being downloaded and the general consensus is "you can't". Now a friend has asked me to think some more about this, and I think that...
7
by: mcollins1989 | last post by:
Draw a checkerboard (8 by 8 squares) out of vertical lines and underscores. You must use two nested for loops, and the for loops should each execute 8 times, e.g. for (int x = 0; x < 8; x++) {...
15
by: fyi | last post by:
My new blog on bits and pieces of PHP coding. http://bitspiecesphp.blogspot.com/ Have used all of them. Hope they're as useful to others: Use PHP to track email sender from your web site...
4
by: arturo trafny | last post by:
I am getting the following error: ##this is the error message i receive Traceback (most recent call last): File "C:\Users\a\Desktop\checkerboard", line 26, in <module> ...
0
by: dseals22 | last post by:
Here is the HTML file @model CheckerBoard.Models.HomeModel <html> <head> <title>@Model.PageTitle</title> <link href="~/Styles/Home.css" rel="stylesheet" /> </head>
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
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...
0
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...

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.