정수 인 경우이 정수에서 정수를 사용하는 경우 Im이라는 문자가 정수인 경우 작업이 훨씬 쉽습니다. 이 배열에서 각 위치를 46 (ASCII는 '.')에 할당하고 해당 ASCII의 (char) 버전을 출력합니다. 내가 보드를 int로 만들었던 이유는 내가이 보드에 숫자를 올릴 것이기 때문이다. 그러나 나는 '.'을 유지하려고한다. 나는 인쇄한다.C에서 char 배열의 문자를 인쇄하는 방법
전류 출력 :
. . . . .
. . . . .
. . . . .
. . . . .
☺ . . . .
원하는 출력 : (문자)로 캐스팅
. . . . .
. . . . .
. . . . .
. . . . .
1 . . . .
인쇄 것 '을.' (ASCII 46) 괜찮지 만 현재 출력에 따라 숫자가 달라집니다.
어떻게 해결할 수 있습니까? 나는 이것을 영원히 꼼짝 않고 바라 보았다.
내 코드 :
using namespace std;
#include <stdlib.h>
#include <iostream>
#include <time.h>
#include <math.h>
void addRandomNumberToBoard(int *board, int &arrSize)
{
srand ((int)time(0)); //seed random
int destination;
do destination= rand() % (arrSize-1);
while (board[destination] != '.'); // place on a random spot on the board
int randomNumber=rand() < RAND_MAX/2 ? 1 : 2; //generate number 1 or 2
board[ destination] = randomNumber; //place the number there
}
int printBoard(int &i, int &arrSize, int *board)
{
for(i=0; i<arrSize; i++)
{
if(i % 5 == 0 && i!=0) //after every 5 positions print a new line
cout<<"\n\n";
cout<<" "<<(char)board[i]; //this MIGHT be the problem
}
}
int main()
{
int i;
int arrSize=25;
int board[arrSize];
for(i=0; i<arrSize; i++) //declare all positions as ASCII '.'
board[i]='.'; //this MIGHT be the other problem
addRandomNumberToBoard(board,arrSize);
printBoard(i, arrSize, board);
}
2048과 비슷한 게임을 만들고 있다고 가정 해 보겠습니다. char 배열은 8보다 큰 수를 가질 수 없으므로 int 배열을 사용하게됩니다. – user3517150