C/C++ Help
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
 
User Name:
Password:
Remember me
 
Go Back   Dev Articles Community ForumsProgrammingC/C++ Help

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Display Modes
 
Unread Dev Articles Community Forums Sponsor:
  #1  
Old March 19th, 2006, 08:05 PM
sh4d0w_aLch3mis sh4d0w_aLch3mis is offline
Registered User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Mar 2006
Posts: 6 sh4d0w_aLch3mis User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 49 m 22 sec
Reputation Power: 0
Array and Character? problem

Alrite, i took a semester of C++ and learned the basics, but i got bored and decided to write a program that generates a random chess position and now im stuck. The theory behind my code was that id create the entire board as a char array, with the white rook as position 0 (which is a1 on a real board) and the rest of the numbers falling in order going horizontally across the board (so the other white rook on a8 is position 7 and the black ones are 56 adn 63). then i created another array of integer type and generated random integers 0-63 for each of its elements. these i used for the locations of each of the original board's elements in the new generated board. I think i made a mistake in attempting to store char strings like "wR" (for white rook) as the elements of a character array. Heres the code regardless:

Code:
#include <iostream.h>
#include <iomanip.h>
#include <windows.h>
#include <stdlib.h>
#include <string.h>
#include <stdio.h>
#include <time.h>
#include <dos.h>
  

int main()
{
      char open[3] = "  ";
      char wpawn[3] = "wp", wknight[3] = "wk", wbishop[3] = "wb", wrook[3] = "wR", wqueen[3] = "wQ", wking[3] = "wK";
      char bpawn[3] = "bp", bknight[3] = "bk", bbishop[3] = "bb", brook[3] = "br", bqueen[3] = "bQ", bking[3] = "bK";

      char newboard[64];
      int randboard[64];
      int radnum;
      char board[64];

      board[0] = *wrook;
      board[1] = *wknight;
      board[2] = *wbishop;
      board[3] = *wqueen;
      board[4] = *wking;
      board[5] = *wbishop;
      board[6] = *wknight;
      board[7] = *wrook;
      board[8] = *wpawn;
      board[9] = *wpawn;
      board[10] = *wpawn;
      board[11] = *wpawn;
      board[12] = *wpawn;
      board[13] = *wpawn;
      board[14] = *wpawn;
      board[15] = *wpawn;

      board[63] = *brook;
      board[62] = *bknight;
      board[61] = *bbishop;
      board[60] = *bking;
      board[59] = *bqueen;
      board[58] = *bbishop;
      board[57] = *bknight;
      board[56] = *brook;
      board[55] = *bpawn;
      board[54] = *bpawn;
      board[53] = *bpawn;
      board[52] = *bpawn;
      board[51] = *bpawn;
      board[50] = *bpawn;
      board[49] = *bpawn;
      board[48] = *bpawn;
                   
      int o, p, k, l;
      
      for(o=16; o<48; o++)
      {
               board[o] = *open;
      }
      
      srand( time(NULL));
      
      cout << "Its the random chess position generator!!"
           << "\nPress Enter to Generate a random chess position.";
      getchar();
      system("cls");
      
      for(p=0; p<64; p++)
      {
               radnum = rand()%64 + 1;  
               if (radnum != randboard[0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,  20,21,22,23,24,25,26,27,28,29,30,31,32,33,34,35,36  ,37,38,39,40,41,42,43,44,45,46,47,48,49,50,51,52,5  3,54,55,56,57,58,59,60,61,62,63]) randboard[p] = radnum;
      }
      
      for(k=0; k<64; k++)
      {
               newboard[randboard[k]] = board[k];
      }
      
      for (l=0; l<64; l++)
      {
                cout << newboard[l] << open;
                if ((l%7) == 0) cout << endl << endl;
      }
      
      
      getchar();
      return 0;
}



i kno i kno, its not pretty to look at, especially the part where i check if the random number is already used, but its been a while since ive coded anything, so i was impressed the thing even ran. when it does run, i get odd characters like w's and b's and smiley faces in something somewhat resembling a chess board. Any ideas about wahts goin wrong with this one?

Reply With Quote
  #2  
Old March 23rd, 2006, 11:09 PM
Cirus Cirus is offline
Contributing User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Mar 2005
Posts: 276 Cirus User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 1 Day 11 h 48 m 58 sec
Reputation Power: 4
In the stmts. where you assign each position of board to peg ( that is wb , bp, ..) ,
ex:
Code:
board[0] = *wrook
, here only first character of white rook will be assigned.
i.e board[0] will be 'w' instead of 'wr' as you intend to do.

Similarly for all black colore pegs you get character 'b' on board .

Thirdly, you are initializing rest of the spaces with white spaces characters.Here again , you are assigning a string 9 a white space string).

Now Ascii value of a blank string is out of range in character value domain.In short value of " " > 256 while that of ' ' < 256.

That is ' ' is a NULL character with AScii value 0 while " " is not a NULL character and has value > 256. As a result you get a graphical face.

Following is the chess board you intend to draw on screen ( The assymetry is due to different width of characters 'w' and 'b') .

wwwwwwwwwwwwwwww
wwwwwwwwwwwwwwww





bbbbbbbbbbbbbbb
bbbbbbbbbbbbbbb

Reply With Quote
Reply

Viewing: Dev Articles Community ForumsProgrammingC/C++ Help > Array and Character? problem


Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump

 Free IT White Papers!
 
How to Present Effectively Online
This white paper offers practical and actionable advice on the key steps that any presenter should consider as they plan and execute a Webinar or online meeting.

Request Your Free Technology Downloads!
 
Open Source Security Myths
Open Source Software (OSS) is computer software whose source code is available to the general public with relaxed or non-existent intellectual property restrictions (or arrangement such as the public domain), and is usually developed with the input of many contributors.

Request Your Free Technology Downloads!
 
Power and Cooling Capacity Management for Data Centers
This paper describes the principles for achieving power and cooling capacity management.

Request Your Free Technology Downloads!
 
Scalable, Fault-Tolerant NAS for Oracle - The Next Generation
For several years NAS has been evolving as a storage alternative for Oracle databases, and for good reason: NAS is quite often the simplest, most cost-effective storage approach for Oracle. Learn about the benefits that HP's approach to scalable NAS brings to Oracle environments in this comprehensive white paper.

Request Your Free Technology Downloads!
 
Understanding Web Application Security Challenges
This white paper discusses many common threats and preventive measures for Web application security, and explains what you can do to help protect your organization.

Request Your Free Technology Downloads!
 

Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 





© 2003-2008 by Developer Shed. All rights reserved. DS Cluster 3 hosted by Hostway
Stay green...Green IT