473,626 Members | 3,201 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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

74 New Member
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 1209

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

Similar topics

36
9456
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
16706
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
2006
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 argument for *arg4? ## ============================================================ def argPrecedence(par1, par2=0, par3=0, *par4, **par5): print 'par1 =', par1, ' # positional argument' print 'par2 =', par2, ' # keyword argument'
15
4505
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 I might have a solution. It is somewhat complicated, but it probably ought to work.... Forget disabling right click menus and the like, it seems to me that the problem is screen capture.
7
2780
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++) { for (int y = 0; y < 8; y++) { // print out one square of the checkerboard } }
15
2557
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 http://bitspiecesphp.blogspot.com/2008/06/use-php-to-track-email-sender-from-your.html Using PHP to solve the session_start() Error
4
4074
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> print(checkerboard(plaintext)) File "C:\Users\a\Desktop\checkerboard", line 20, in checkerboard ciphertext=ciphertext+idx TypeError: Can't convert 'int' object to str implicitly
0
988
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
8265
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8196
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 synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8705
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. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8637
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 captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
8364
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 most users, this new feature is actually very convenient. If you want to control the update process,...
0
7193
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 launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6125
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 instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
1
2625
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
1511
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.