Hello I am trying to create a tick Tack Toe Game For my College Project,
The Board size of the game needs to be GENERIC using 2D array in C++.
So I'm having trouble while initializing Default numbers(Places) identifier in an array
for (int i = 0; i < SIZE; i++)
{
for (int j = 0; j < SIZE; j++)
{
Boards[i][j] = initial++;
}
}
for (int i = 0; i < SIZE; i++)
{
for (int j = 0; j < SIZE; j++)
{
if (Boards[i][j] < 10) cout << " " << Boards[i][j] << " | ";
else cout << Boards[i][j] << " | ";
}
cout << endl;
}
As the variable 'initial' is a integer and i have to increment it in loop.I am quite not sure how to save it in char array (BOARD) Board needs to be char to display X,O
Now that you have posted the full code, I can see a problem on this line and the other one like it:
cout << Boards[i][j] << " | ";
Since the type of Boards[i][j] is a char, the C++ standard library will just send that char to your terminal, and the terminal will try to interpret it as an ASCII character. You need to cast it to an int first so that the C++ standard library will format it properly for you:
cout << (int)Boards[i][j] << " | ";
Related
I am struggling with printing an array with 4 rows and 4 columns, when I initialized the array and entered all the values. Then, I used for loop to get all the values together so I can print them. But I get is an array that companied all the values in one row.
I have attached the output when I run the code.
Here is a portion of my code, it is long code but I am struggling in specific part:
#include <iostream>
using namespace std;
int main()
{
cout << "The martix before I flipped it: " << endl;
cout << endl;
int array[4][4] = { 16,3,2,13,5,10,11,8,9,6,7,12,4,5,14,1 };
for (int i = 0; i < 4; i++) {
for (int j = 0; j < 4; j++) {
cout << array[i][j] << " ";
}
}
return 0;
The standard output utility std::cout is a stream from the stl and, as such, its << operator does not usually automagically append a linebreak.
Which is quite practical since, otherwise, you would not be able to print multiple numbers on a single line.
That being, said, you'll need to add the linebreak manually, like so :
for (int i = 0; i < 4; i++) {
for (int j = 0; j < 4; j++) {
std::cout << array[i][j] << " ";
}
std::cout << std::endl;
}
Alternatively, you can consider printing lines 4 at a time since your matrix is of constant size :
for (int i = 0; i < 4; i++) {
std::cout << array[i][0] << " "
<< array[i][1] << " "
<< array[i][2] << " "
<< array[i][3] << " " << std::endl;
}
Have a great day,
I am trying to assign a specific part of an array a new value, but it doesn't seem to be inserting the new value into the array.
char matrix[20][8] = {/*160 * '#'*/};
void Draw() {
system("CLS");
cout << "Welcome to Primitive Pong v1.0!" << endl;
for (int i = 0; i < 8; i++) {
cout << endl;
for (int j = 0; j < 20; j++) {
cout << matrix[i][j] << " ";
}
}
}
while (gameOver == false) {
matrix[10][4] = 'O';
Draw();
this_thread::sleep_for(chrono::milliseconds(1000));
}
I expect this to output a grid of 160 "#" with a "O" near the middle, but instead it just prints 160 "#". I am trying to make a game of console pong. I have tried using 'matrix[10][4] = {'O'};, but that does nothing different.
The problem is that you declare matrix[20][8] but then you access it as if its dimensions are [8][20] instead.
The total is the same but the access doesn't work correctly and, unfortunately, C++ will not check about this kind of mistake. Changing the code to
cout << matrix[j][i] << " ";
should make things work a you expect.
I'm trying to make a dungeon crawlesque game and I have this code to create a game board. I'm using 'F' as the finish point and 'P' for the player.
void Gameboard::CreateGameboard()
{
for (int i = 0; i < 10; i++)
{
for (int j = 0; j < 10; j++)
{
GameGrid[i][j] = 'x';
}
}
cout << " 0 1 2 3 4 5 6 7 8 9 10" << endl;
cout << " +---------------------+" << endl;
for (int i = 0; i < 10; i++)
{
cout << " " << "|" << GameGrid[i][0];
for (int j = 0; j < 10; j++)
{
if (i == Spawn[0] && j == Spawn[0])
{
GameGrid[0][0] = 'P';
}
cout << " " << GameGrid[i][j];
}
cout << "|" << endl;
}
cout << " +---------------------+" << endl;
}
The problems I'm facing are. 'P' is being placed in the first two slots of the board and unsure why. And how would I update the board with player movement? I have a Player class with x,y position variables and my thought is to increment down/up based on where they're going. Is it required to reprint the whole board after every movement?
With your drawing of the board.
for (int i = 0; i < 10; i++)
{
cout << " " << "|" << ***GameGrid[i][0]***;
for (int j = 0; j < 10; j++)
{
cout << " " << GameGrid[i][j];
}
cout << "|" << endl;
}
You print out the first item of the row, then print the whole row, including the first item again. so each row will have a double up of the first item.
As for your second question, clearing the screen is exactly what you'll have to do. If you're using windows then you can use system("cls"); to 'clear' the console and then redraw. I would recommend putting the board drawing and board creation into different functions.
I wrote a program to accept 15 integer values in an array, then pass this array to a function which will multiply each even index value by 4.
Currently the program displays the initial array, but seems like it's getting hung up before it displays the modified array.
Please help me understand why the program is getting stuck here!
int main(){
const int SIZE = 15;
int quad[SIZE] = {};
void quadruple(int[], const int);
cout << "Enter 15 integer values into an array." << endl;
for (int i = 0; i < SIZE; i++) // Accept 15 int values
{
cout << i << ": ";
cin >> quad[i];
}
cout << "Before quadruple function is called: " << endl;
for (int i = 0; i < SIZE; i++)
{
cout << quad[i] << " ";
}
cout << endl;
quadruple(quad, SIZE);
cout << "After even index value multiplication: " << endl;
for (int i = 0; i < SIZE; i++)
{
cout << quad[i] << " ";
}
cout << endl;
return 0;
}
void quadruple(int values[], const int SZ){
for (int i = 0; i < SZ; i + 2) // Multiply even values by 4
{
if ((i % 2) == 0)
{
values[i] = values[i] * 4;
}
else // Keep odd values the same
{
values[i] = values[i] * 1;
}
}
}
for (int i = 0; i < SZ; i + 2)
"i + 2" doesn't do anything.
You probably meant "i += 2;".
Your homework assignment is to find some documentation about your system's debugger. And find where your rubber duck is, as it's been suggested in the comments.
I am running a program in c++ which prints the multiplication table from 1 to 40 but it starts from 13*10=130 to 40 so whats the reason behind this?
Below is the formatted version of the code you posted:
#include<iostream>
using namespace std;
int main() {
for (int i = 1; i <= 40; i++) {
for (int j = 1; j <= 10; j++) {
cout << i << " * " << j << " = " << i*j << endl;
}
cout << endl;
}
return 0;
}
It starts printing at 13 * 10. What's the reason for this?
Notably, we can see the variables of both loops (i and j) are initialized to 1 when the loop starts. Because of this, you would be right in expecting the first loop-through to print 1 * 1 = 1.
This suggests, as PRIME has pointed out, that the environment (such as the Windows Console) that you are printing to may not have a large enough buffer to store and display the 440 lines of output the program will try to print.
How can I get around this?
You can try re-sizing your printing environment's internal buffer (if it allows) to allow for 440 lines of print. In MS-DOS, for example, you can manually change this by right-clicking the title-bar, going into Properties, then the Layout tab, and changing the Screen Buffer width and height to values that suit.
Alternatively, you could conserve print-space by replacing endl statements with regular spaces, a la:
for (int i = 1; i <= 40; i++) {
for (int j = 1; j <= 10; j++)
cout << i << " * " << j << " = " << i*j << ' ';
}
You also have the option of outputting to a file instead of your current printing environment:
#include <fstream>
using namespace std;
int main() {
ofstream Output("Output.txt"); //Creates a file "Output.txt"
if (Output.is_open()) { //If the file is open, proceeed
for (int i = 1; i <= 40; i++) {
for (int j = 1; j <= 10; j++)
Output << i << " * " << j << " = " << i*j << '\n';
Output << '\n'; //^^^Write multiplication table to the file
}
}
return 0;
}