| |||||||||
![]() |
|
|
«
Previous Thread
|
Next Thread
»
|
Thread Tools | Search this Thread | Display Modes |
|
#1
|
|||
|
|||
|
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? |
|
#2
|
|||
|
|||
|
In the stmts. where you assign each position of board to peg ( that is wb , bp, ..) ,
ex: Code:
board[0] = *wrook 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 |
![]() |
| Viewing: Dev Articles Community Forums > Programming > C/C++ Help > Array and Character? problem |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|
|
|
|