Troubles with battleship game in C - c

So i've been trying to write a sink the battleship game in C. I already wrote a simple version with randomly generated booleans however i was not happy with ships being only one block in size and there were too many of them, but i digress.
Here i've wrote what i believe is a messy piece of code, and it works, sometimes...
Here it is:
void generate_field(int *i, int *j, int n)
{
*i=rand()%n;
*j=rand()%n;
}
void map_gen(struct game *data,int n)
{
int i,j,k,l;
int return_value=0;
for(i=0;i<n;i++)
{
for(j=0;j<n;j++)
{
data->tiles[i][j].ship=0;
data->tiles[i][j].uncovered=0;
}
}
// **4**
generate_field(&k,&l,n);
if(k==0 || k==1)
{
data->tiles[k][l].ship=4;
data->tiles[k+1][l].ship=4;
data->tiles[k+2][l].ship=4;
data->tiles[k+3][l].ship=4;
data->shipcount++;
}
else if(k==(n-1) || k==(n-2))
{
data->tiles[k][l].ship=4;
data->tiles[k-1][l].ship=4;
data->tiles[k-2][l].ship=4;
data->tiles[k-3][l].ship=4;
data->shipcount++;
}
else if(l==0 || l==1)
{
data->tiles[k][l].ship=4;
data->tiles[k][l+1].ship=4;
data->tiles[k][l+2].ship=4;
data->tiles[k][l+3].ship=4;
data->shipcount++;
}
else if(l==(n-1) || l==(n-2))
{
data->tiles[k][l].ship=4;
data->tiles[k][l-1].ship=4;
data->tiles[k][l-2].ship=4;
data->tiles[k][l-3].ship=4;
data->shipcount++;
}
// **3**
do{
generate_field(&k,&l,n);
}while(data->tiles[k][l].ship!=0 && (data->tiles[k+1][l].ship!=0 || data->tiles[k-1][l].ship!=0 || data->tiles[k][l+1].ship!=0 || data->tiles[k][l-1].ship!=0) && (data->tiles[k+2][l].ship!=0 || data->tiles[k-2][l].ship!=0 || data->tiles[k][l+2].ship!=0 || data->tiles[k][l-2].ship!=0));
if((k==0 || k==1) && (data->tiles[k+1][l].ship==0 && data->tiles[k+2][l].ship==0))
{
data->tiles[k][l].ship=3;
data->tiles[k+1][l].ship=3;
data->tiles[k+2][l].ship=3;
data->shipcount++;
}
else if((k==(n-1) || k==(n-2)) && (data->tiles[k-1][l].ship==0 && data->tiles[k-2][l].ship==0))
{
data->tiles[k][l].ship=3;
data->tiles[k-1][l].ship=3;
data->tiles[k-2][l].ship=3;
data->shipcount++;
}
else if((l==0 || l==1) && (data->tiles[k][l+1].ship==0 && data->tiles[k][l+2].ship==0))
{
data->tiles[k][l].ship=3;
data->tiles[k][l+1].ship=3;
data->tiles[k][l+2].ship=3;
data->shipcount++;
}
else if((l==(n-1) || l==(n-2)) && (data->tiles[k][l-1].ship==0 && data->tiles[k][l-2].ship==0))
{
data->tiles[k][l].ship=3;
data->tiles[k][l-1].ship=3;
data->tiles[k][l-2].ship=3;
data->shipcount++;
}
// **2**
do{
generate_field(&k,&l,n);
}while(data->tiles[k][l].ship!=0 && (data->tiles[k+1][l].ship!=0 || data->tiles[k-1][l].ship!=0 || data->tiles[k][l+1].ship!=0 || data->tiles[k][l-1].ship!=0));
if((k==0 || k==1) && (data->tiles[k+1][l].ship==0))
{
data->tiles[k][l].ship=2;
data->tiles[k+1][l].ship=2;
data->shipcount++;
}
else if((k==(n-1) || k==(n-2)) && (data->tiles[k-1][l].ship==0))
{
data->tiles[k][l].ship=2;
data->tiles[k-1][l].ship=2;
data->shipcount++;
}
else if((l==0 || l==1) && (data->tiles[k][l+1].ship==0))
{
data->tiles[k][l].ship=2;
data->tiles[k][l+1].ship=2;
data->shipcount++;
}
else if((l==(n-1) || l==(n-2)) && (data->tiles[k][l-1].ship==0))
{
data->tiles[k][l].ship=2;
data->tiles[k][l-1].ship=2;
data->shipcount++;
}
// **1**
do{
generate_field(&k,&l,n);
}while(data->tiles[k][l].ship!=0);
data->tiles[k][l].ship=1;
data->shipcount++;
}
the **#** are ship sizes.
the int n is the size of a dimension of the matrix array(i have two sizes:Normal which is 5x5 and large which is 8x8)
Anyway i know this could be written in a way simpler way and that it could actually work. The do-while loops are way too long and a lot of the times one or two my ships don't generate. I think it's because i somewhat limited their spawn by using(k==0 or k==n-1) stuff, however i have no idea what to do. Can anyone here give me some hints of how could have i written this differently and more compact and in a way in which it actually works right?

The problem is with how you determine location and direction for the ship.
I would do like this:
void place_ship(struct game* data, int n, int shipsize)
{
int x = 0, y = 0; // Uesd for ship direction
// Generate a direction
if (rand()%2) {
i=rand()%(n - shipsize + 1);
j=rand()%n;
x = 1;
y = 0;
} else {
i=rand()%n;
j=rand()%(n - shipsize + 1);
x = 0;
y = 1;
}
for (k = 0; k < shipsize; k++) {
if (data->tiles[i + x * k][j + y * k].ship != 0) {
// Space already occupied - retry!
return place_ship(data, n, shipsize);
}
}
for (k = 0; k < shipsize; k++) {
data->tiles[i + x * k][j + y * k].ship = shipsize;
}
}

Related

if statement doesn't work on a triangle type function

I'm a complete newbie, so please excuse me.
I tried using online compiler but they unresponsive, and I get no return value (or return 0 for whatever I enter)
I tried to write a function that check if triangle is right, isosceles or both, and return 1,2,3 respectively, all other cases should return 0.
int main() {
int TriangleType(unsigned angle1, unsigned angle2) {
unsigned angleSum = angle1 + angle2;
if (angleSum >= 180) {
return 0;
}
/* if triangle is right ---> */
if (angle1==90 || angle2==90 || angleSum==90) {
/*if it is also an isosceles --->*/
if (angle2==45 || angle1==45) {
return 3;
}
return 1;
}
/*check if it only a isosceles*/
if (angle1==(180-angle2)/2 ||
angle2== (180-angle1)/2 ||
angle1==angle2) {
return 2;
}
return 0;
}
TriangleType(110, 111);
}
First, don't try to use nested functions in C. Pulling that function out of main.
int TriangleType(unsigned angle1, unsigned angle2) {
unsigned angleSum = angle1 + angle2;
if (angleSum >= 180) {
return 0;
}
/* if triangle is right ---> */
if (angle1==90 || angle2==90 || angleSum==90) {
/*if it is also an isosceles --->*/
if (angle2==45 || angle1==45) {
return 3;
}
return 1;
}
/*check if it only a isosceles*/
if (angle1==(180-angle2)/2 ||
angle2== (180-angle1)/2 ||
angle1==angle2) {
return 2;
}
return 0;
}
int main() {
TriangleType(110, 111);
}
Second, this doesn't do anything with the return value from the function, so of course you see no output.
int main(void) {
switch (TriangleType(110, 111)) {
case 1:
printf("Right triangle\n");
break;
case 2:
printf("Isosceles triangle\n");
break;
case 3:
printf("Both types\n");
break;
default:
printf("None of the above\n");
}
return 0;
}
Code is functionally wrong
Even after re-organizing and moving the nested function, code attempts to be clever and not perform simple tests.
if (angle1 == (180 - angle2) / 2 || angle2 == (180 - angle1) / 2 || angle1 == angle2) fails in cases like TriangleType(45,89) --> 2 as (180 - angle2) / 2 rounds down. angle1 * 2 == (180 - angle2) would make more sense.
TriangleType(90, 90), where the 3rd angle is 0, returns 0. That is asymmetric. TriangleType(0, 90), TriangleType(90, 0) do not return 0.
TriangleType(0, 0) returns 2. I would expect that to return 0, a rejected triangle.
Below is a test harness for OP to use and test TriangleType().
#include <stdio.h>
int TriangleType(unsigned angle1, unsigned angle2) {
unsigned angleSum = angle1 + angle2;
if (angleSum >= 180) {
return 0;
}
/* if triangle is right ---> */
if (angle1 == 90 || angle2 == 90 || angleSum == 90) {
/*if it is also an isosceles --->*/
if (angle2 == 45 || angle1 == 45) {
return 3;
}
return 1;
}
/*check if it only a isosceles */
if (angle1 == (180 - angle2) / 2 || angle2 == (180 - angle1) / 2
|| angle1 == angle2) {
return 2;
}
return 0;
}
int my_TriangleType(unsigned angle1, unsigned angle2) {
unsigned angle3 = 180 - angle1 - angle2;
if (angle1 >= 180 || angle2 >= 180 || angle3 >= 180) {
return 0;
}
if (angle1 == 0 || angle2 == 0 || angle3 == 0) {
return 0;
}
int retval = angle1 == 90 || angle2 == 90 || angle3 == 90 ? 1 : 0;
if (angle1 == angle2 || angle2 == angle3 || angle3 == angle1)
retval += 2;
return retval;
}
#include <string.h>
#define MAX_LIMIT 127
int main() {
printf("score:%d my_score:%d\n", TriangleType(45,89), my_TriangleType(45,89));
int error_count = 0;
int flags[4][4] = {0};
for (unsigned angle1 = 0; angle1 <= 181; angle1++) {
for (unsigned angle2 = 0; angle2 <= 181; angle2++) {
int score = TriangleType(angle1, angle2);
int my_score = my_TriangleType(angle1, angle2);
if (score != my_score) {
error_count++;
if (flags[score][my_score] == 0) {
flags[score][my_score] = 1;
printf("%3d angle1:%2u angle2:%2u score:%d my_score:%d\n", //
error_count, angle1, angle2, score, my_score);
}
}
}
}
printf("total errors:%d\n", error_count);
return 0;
}
Output:
score:2 my_score:0
1 angle1: 0 angle2: 0 score:2 my_score:0
2 angle1: 0 angle2:90 score:1 my_score:0
total errors:181
another_TriangleType() could be made that is more efficient than my_TriangleType(), yet it would needed to functionally match.
Well your questions is lacking some information, but I assume you got as inputs of your program, two angles.
THe fisrt thing we need to think is:
If you got a right triangle, than there is one of those values equals 90 degrees, instead the sum of them must be 90.
On the other hand, if you got an isosceles triangle you should have 2 equal values, instead you must have the 180 deg - sum of both angles should result in one of those two.
Let's try to code this out:
#include <stdio.h>
#include <stdlib.h>
#define TRUE 1
#define FALSE 0
#define RIGHT 0x01
#define ISOSCELES 0x02
#define BOTH 0x03
// If you got a right triangle, than there is one of those values equals 90 degrees, instead the sum of them must be 90.
int bIsRightTriangle(int iAngDeg1, int iAngDeg2){
if ( iAngDeg1 == 90 || iAngDeg2 == 90 )
return TRUE;
if ( (iAngDeg1 + iAngDeg2) == 90 )
return TRUE;
return FALSE;
}
// On the other hand, if you got an isosceles triangle you should have 2 equal values,
// instead you must have the 180 deg - sum of both angles should result in one of those two.
int bIs_IsoscelesTriangle(int iAngDeg1, int iAngDeg2){
int iAngDeg3 = 180 - iAngDeg1 + iAngDeg2;
if ( iAngDeg1 == iAngDeg2 )
return TRUE;
if ( (iAngDeg3 == iAngDeg1) || (iAngDeg3 == iAngDeg2) )
return TRUE;
return FALSE;
}
int iGetTriangleTypeByAngles(int iAngDeg1, int iAngDeg2){
int iReturnType = 0;
iReturnType = bIsRightTriangle(iAngDeg1, iAngDeg2) ? (iReturnType | RIGHT) : iReturnType;
iReturnType = bIs_IsoscelesTriangle(iAngDeg1, iAngDeg2) ? (iReturnType | ISOSCELES) : iReturnType;
return iReturnType;
}
int main(int argc, char *argv[]){
if ( argc < 3 ){
return -1;
}
// In case you want an output
// printf("%d\n", iGetTriangleTypeByAngles(atoi(argv[1]), atoi(argv[2])));
return iGetTriangleTypeByAngles(atoi(argv[1]), atoi(argv[2]));
}

Why this code resulted a segmentation fault?

i am writing a simple simulator of spilling eggs in а pan with the flood fill algorithm in C. So the pan is a char matrix, and with '#' are his walls,'.' means that the place is empty and 'O' that it has a egg that will spill.
That is an example of what that program should do.
10 10
..##......
.#O.#.....
.#..#..#..
..##..#.#.
.......#..
..#####...
..#...#...
..#...##..
...##..#..
....####..
⇨
..##......
.#OO#.....
.#OO#..#..
..##..#.#.
.......#..
..#####...
..#***#...
..#***##..
...##**#..
....####..
i have written that:
#include <stdio.h>
void food_fill(int,int,char [1000][1000],int,int,char);
int main(){
int row,col,rows,cols;
scanf("%d %d",&rows,&cols);
getchar();
char matrix [rows][cols];
for(row = 0;row < rows;row++){
for(col = 0;col < cols;col++){
scanf("%c",&matrix[row][col]);
}
getchar();
}
int foodX=0,foodY=0;
char foodChar='s';
for(row=0;row<rows;row++){
for(col = 0;col < cols;col++){
if(matrix[row][col] == 'O'){
foodX = row;
foodY = col;
foodChar = matrix[row][col];
}
}
}
food_fill(rows,cols,matrix,foodX,foodY,foodChar);
for(row = 0;row < rows;row++){
for(col = 0;col <cols;col++){
printf("%c",matrix[row][col]);
}
printf("\n");
}
return 0;
}
void food_fill(int rows,int cols,char matrix[rows][cols],int foodX,int foodY,char foodChar){
int r=rows,c=cols;
if(matrix[foodX][foodY] == '#')return;
if(foodX < 0 || foodX >= rows || foodY < 0 || foodY >= cols)return;
matrix[foodX][foodY] = foodChar;
food_fill(r,c,matrix,foodX+1,foodY,foodChar);
food_fill(r,c,matrix,foodX-1,foodY,foodChar);
food_fill(r,c,matrix,foodX,foodY+1,foodChar);
food_fill(r,c,matrix,foodX,foodY-1,foodChar);
}
It is not completed yet but the food_fill() function resulted in segmentation fault and i don't know why.
if(matrix[foodX][foodY] == '#')return;
if(foodX < 0 || foodX >= rows || foodY < 0 || foodY >= cols)return;
Order is wrong. As a result you are accessing invalid indices. Giving you segmentation fault. Correct one would be
if(foodX < 0 || foodX >= rows || foodY < 0 || foodY >= cols)return;
if(matrix[foodX][foodY] == '#')return;
Also there are bugs on many levels.
You are having an infinite loop due to repetitive similar calls.
int dx[]={1,0,-1,0};
int dy[]={0,1,0,-1};
void food_fill(int rows,int cols,char matrix[][cols],int foodX,int foodY,char foodChar){
matrix[foodX][foodY] = foodChar;
for(int i=0;i<4;i++){
if( foodX+dx[i]>=0 && foodX+dx[i]<rows && foodY+dy[i] >=0 && foodY+dy[i]<cols && matrix[foodX+dx[i]][foodY+dy[i]] != '#' && matrix[foodX+dx[i]][foodY+dy[i]]!=foodChar){
food_fill(rows,cols,matrix,foodX+dx[i],foodY+dy[i],foodChar);
}
}
}
The main condition change is
if( foodX+dx[i]>=0 && foodX+dx[i]<rows && foodY+dy[i] >=0 && foodY+dy[i]<cols && matrix[foodX+dx[i]][foodY+dy[i]] != '#' && matrix[foodX+dx[i]][foodY+dy[i]]!=foodChar){

Tic-Tac-Toe without AI

I'm doing homework for UNI and I got to do a Tic-Tac-Toe without any decision taken by player, the moves are all chosen randomly. So if the character on matrix is ' ' it means it's free, while if it's 'X' or 'O' it should generate another move. This is the code (language C):
if (playerTurn == 1){
playerSymb = 'X';
}
else if (playerTurn == 2){
playerSymb = 'O';
}
if (matrix[rand1][rand2] == ' '){
matrix[rand1][rand2] = playerSymb;
} else if(matrix[rand1][rand2] == 'X' || matrix[rand1][rand2] == 'O'){
do{
randAlt1 = MINRND + rand()%(MAXRND - MINRND +1);
randAlt2 = MINRND + rand()%(MAXRND - MINRND +1);
}while (matrix[randAlt1][randAlt2] != 'X' && matrix[randAlt1][randAlt2] != 'O');
matrix[randAlt1][randAlt2] = playerSymb;
}
I did not copied the whole code because it's not finished at all, i just need help solving this. But if I try to run this, the Symbols can be overwritten, like if I have a 'X' at matrix[1][2], it's possible that it will be a 'O' after some turns. So how can I make moves do not overwrite? (sorry for bad english).
Just put correct condition:
while (matrix[randAlt1][randAlt2] == 'X' || matrix[randAlt1][randAlt2] == 'O')
(i.e. try again if this cell is not empty)
Also it is easy to simplify your code without loosing of anything:
randAlt1 = rand1;
randAlt2 = rand2;
while (matrix[randAlt1][randAlt2] != ' ') {
randAlt1 = MINRND + rand()%(MAXRND - MINRND +1);
randAlt2 = MINRND + rand()%(MAXRND - MINRND +1);
}
matrix[randAlt1][randAlt2] = (playerTurn == 1) ? 'X' : 'O';
And it is better to add loop guard to prevent infinite loop (or to add special checks for this case):
randAlt1 = rand1;
randAlt2 = rand2;
int nbAttempts = 0;
while (matrix[randAlt1][randAlt2] != ' ' && nbAttempts < 100) {
randAlt1 = MINRND + rand()%(MAXRND - MINRND +1);
randAlt2 = MINRND + rand()%(MAXRND - MINRND +1);
nbAttempts++;
}
if (matrix[randAlt1][randAlt2] != ' ') {
// show error message and stop the game
}
matrix[randAlt1][randAlt2] = (playerTurn == 1) ? 'X' : 'O';
You choose an arbitrary position and then test if it is free – possibly multiple times. But you can also choose a number of a free position and then find it.
First set up a turn counter
int turnNo = 0;
then make a loop for alternate moves, which chooses one of 9-turnNo unused positions, finds it, marks is with a player mark and tests if the move made a line of three:
while(turnNo < 9)
{
char currPlayerMark = ...choose 'X' or 'O';
int freePos = 9 - turnNo;
int currPos = rand() % freePos; // 0 .. freePos-1
for(x=0; x<3; x++)
{
for(y=0; y<3; y++)
{
if(matrix[x][y] == ' ') // a free position
if(--currPos < 0) // the sought one
break; // break the inner loop
}
if(currPos < 0)
break; // break the outer loop
}
matrix[x][y] = currPlayerMark;
if(test_for_win_position(x,y))
{
message_a_win_of_player(currPlayerMark);
break; // turnNo < 9 here
}
turnNo ++;
}
Finally test if the loop terminated with no 'win':
if(turnNo == 9)
message_its_a_draw(); // no-one wins
A function to test the win position might look like this:
int test_for_win_position(int x, int y)
{
char mark = matrix[x][y];
// check a column
if(matrix[x][0] == mark && matrix[x][1] == mark && matrix[x][2] == mark)
return 1;
// check a row
if(matrix[0][y] == mark && matrix[1][y] == mark && matrix[2][y] == mark)
return 1;
// check one diagonal
if(x==y)
if(matrix[0][0] == mark && matrix[1][1] == mark && matrix[2][2] == mark)
return 1;
// check another diagonal
if(x+y==2)
if(matrix[0][2] == mark && matrix[1][1] == mark && matrix[2][0] == mark)
return 1;
// current player has not won (yet)
return 0;
}

Checking for valid moves in Reversi for all 8 directions

I have a function to check the valid moves in my reversi game. I look at the unoccupied spaces, and check if an adjacent space in any 8 directions are the opposite piece. (If I'm black, I search for white) Now, if I find a piece that's adjacent, I should keep looking towards that direction and see if my own piece is at the end, then I return true, else if its an unoccupied space or off the board boundaries, I return false.
My function doesn't seem to work properly as I print out the wrong moves.
bool checkLegalInDirection(char boardgame[26][26], int size, int row, int col, char color) {
int currentRow, currentCol;
for (int deltaRow = -1; deltaRow < 2; deltaRow++) {
for (int deltaCol = -1; deltaCol < 2; deltaCol++) {
if (deltaRow == 0 && deltaCol == 0) {
break;
} else {
row = row + deltaRow;
col = col + deltaCol;
if (positionInBounds(size, row, col)) {
while (boardgame[row][col] == OppositeColor(color)) {
currentRow = row + deltaRow;
currentCol = col + deltaCol;
if (positionInBounds(size, currentRow, currentCol)) {
if (boardgame[currentRow][currentCol] == color) {
return true;
} else {
return false;
}
}
}
}
}
}
}
}
deltaRow and deltaCol are the increments that go in each direction and add one time to keep searching in a specified location. PositioninBounds is a function I have to make sure my searches are within the board boundaries. My deltarow and deltacol cannot be both 0 at the same time, so somehow I need to skip that step, (which I probably did wrong). Oppositecolor is a function that returns me the opposite color of my own piece.
I think your code has multiple mistakes.
Your code is incorrectly breaking the for loop when you should be continuing with the next iteration (as mentioned by chux).
Change...
if (deltaRow == 0 && deltaCol == 0) {
break;
} else {
...
}
to either chux's suggestion...
if (deltaRow == 0 && deltaCol == 0) {
continue;
} else {
...
}
or to an even simpler solution...
if (deltaRow != 0 || deltaCol != 0) {
...
}
Inside the deltaRow/deltaCol loops, your code is incorrectly modifying the original row/col values which your code will need in later loop iterations.
You can change...
row = row + deltaRow;
col = col + deltaRow;
to...
currentRow = row + deltaRow;
currentCol = col + deltaRow;
Inside the while loop, your code is incorrectly returning false. You can not return false until you have completed all the for loops.
Before entering the while loop, you need to check that the adjacent space is in bounds and opposite color...
if (positionInBounds(size, currentRow, currentCol) && boardgame[currentRow][currentCol] == OppositeColor(color)) {
If so, then skip over all adjacent opposite colors...
while (positionInBounds(size, currentROw, currentColor) && boadgame[currentRow][currentCol] == OppositeColor(color)) {
{
currentRow = currentRow + deltaRow;
currentCol = currentCol + deltaCol;
}
After you have skipped over opposite colors then you need to check for same color. If so then return true.
if (positionInBOunds(size, currentRow, currentCol) && boardgame[currentRow][currentCol] == color) {
return true;
}
You code should only return false after checking all directions...
for (int deltaRow = -1; deltaRow < 2; deltaRow++) {
for (int deltaCol = -1; deltaCol < 2; deltaCol++) {
....
}
}
return false;

Array Iteration for a program [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
I have a project yet my teacher hasn't taught us about arrays. We need to output =<> signs corresponding to the comparison of one number to another. IE the main number is 1234 and I put in 2315, the output would be <<<> where the signs do not go in the order of the numbers but by this order =, <, >.
I have an idea and that to use an array then to use some code that would read out the whole array and apply rules to it, however I do not know how to implement this. I have been googling for awhile now and nothing I found really helps.
Just to let you know the program has way more steps than just this, all of which I have already completed, I just can't figure out this part. I do not want just the answer, I just want someone to point me in the right direction.
Thanks
EDIT:: The example 1234 and 2315 are bad examples. To give a more definitive idea without giving away too much of the problem so I have work to do is listing num1 and num2 (corresponding to 1234 and 2315) from least to greatest or greatest to least and compare that way. So another example would be 4751 is the main number and I put in 1294. The output would be ==<>. Thanks for the help guys so far. I am learning a lot.
EDIT2:: Thanks guys for the help. I learned a lot. I don't want any more submissions at least until I can upload my code.
Taking you at your word that you've already successfully completed most of your assignment, and by giving you code that you'll have to work through and understand to figure it out and adapt it to your needs, this will do what you want. The fact that you don't have to output the signs in the same order as the numbers themselves is what makes this easier.
#include <stdio.h>
int main(void) {
int num1 = 1234;
int num2 = 2315;
int lt = 0, gt = 0, eq = 0;
while ( num1 > 0 && num2 > 0 ) {
int op1 = num1 % 10;
int op2 = num2 % 10;
if ( op1 < op2 ) {
++lt;
} else if ( op1 > op2 ) {
++gt;
} else {
++eq;
}
num1 /= 10;
num2 /= 10;
}
for ( int i = 0; i < eq; ++i ) {
putchar('=');
}
for ( int i = 0; i < lt; ++i ) {
putchar('<');
}
for ( int i = 0; i < gt; ++i ) {
putchar('>');
}
putchar('\n');
return 0;
}
and outputs:
paul#MacBook:~/Documents/src/scratch$ ./eq
<<<>
paul#MacBook:~/Documents/src/scratch$
This code lets you get the nth digits you can compare and make a count of each symbol you need to return
char nthdigit(int x, int n)
{
while (n--) {
x /= 10;
}
return (x % 10) + '0';
}
And this is how you get the length of a number, check this post
As promised here is the rest of my code. It fixes the issue pointed out in the question but I have another issue. It is probably pretty noticeable but I wanted to post my code so I don't forget again.
#include<stdio.h>//standard inputs and outputs
#include<stdlib.h>//for compare, qsort, srand, and rand function
#include<time.h>//for random numbers at different times
#include<unistd.h>//for fun at the end
int main(void){
int guess, gdig1, gdig2, gdig3, gdig4, random, rdig1, rdig2, rdig3, rdig4;//variables for main
int lt, gt, eq, i, q, temp, holder;//this is for the hints
int g[3],r[3];
printf("Give the computer a few seconds to come up with a super secret passcode.\n");
do{//figuring out a random code
unsigned int iseed = (unsigned int)time(NULL);
srand (iseed);//using an unassigned integer
random = rand()%9000+1000;//generating a random number but limiting it
rdig4 = random%10;
rdig3 = (random/10)%10;
rdig2 = (random/100)%10;
rdig1 = (random/1000)%10;//figuring out the individual digits of the code
} while ((rdig1 == rdig2)||(rdig1 == rdig3)||(rdig1 == rdig4)||(rdig2 == rdig3)||(rdig2 == rdig4)||(rdig3 == rdig4)||(rdig1 == 0)||(rdig2 == 0)||(rdig3 == 0)||(rdig4 == 0));
//^^ some crazy boolean expression making sure the random integer doesnt have any of the same digits.
printf("\nThe actual passcode is:%d.\n",random);//testing in beginning comment out ********
do{
do{
printf("\nEnter in your guess for the passcode: ");
scanf("%d",&guess);//inputting and reading the guessed code
// printf("You entered:%d\n",guess);//just to check comment out at end**
gdig4 = guess%10;
gdig3 = (guess/10)%10;
gdig2 = (guess/100)%10;
gdig1 = (guess/1000)%10;//figuring out the individual digits of the guess code using modulus operator
if (guess > 9999){//the starting loop statement to make sure number is valid. this one is if it is greater than 4 digits
printf("\nPlease use a four digit number for the passcode.\n");
}
else if (guess < 1000){//this one is if it is less than 4 digits
printf("\nPlease use a four digit number for the passcode.\n");
gdig1 = 1;
gdig2 = 1;//used so the computer still loops
gdig3 = 1;
gdig4 = 1;
}
if ((gdig1 == 0) || (gdig2 == 0) || (gdig3 == 0) || (gdig4 == 0)){
break;
}
if ((rdig1 == gdig1) && (rdig2 == gdig2) && (rdig3 == gdig3) && (rdig4 == gdig4)){//to skip this codeblock and move onto next
break;
}
if (guess > 9999){
break;
}
if (guess < 1000){
break;
}
printf("\n%d %d %d %d\n",rdig1,rdig2,rdig3,rdig4); //used to testing comment out at end
printf("\n%d %d %d %d\n",gdig1,gdig2,gdig3,gdig4);
while (guess > 0){
g[i++] = guess % 10;
guess /=10;
}
do{//took a long long LONG time to get
for(i = 0; i<3;i++){
if(g[i] > g[i+1]){
holder = g[i+1];
g[i]=g[i+1];
g[i+1] = holder;
}
}
}while (i == 1);
for(i = 0;i<4;i++){
printf("%d",g[i]);
}
while (random > 0){
r[i++] = random % 10;
random /=10;
}
do{//took a long long LONG time to get
for(i = 0; i<3;i++){
if(r[i] > r[i+1]){
temp = r[i+1];
r[i]=r[i+1];
r[i+1] = temp;
}
}
}while (i == 1);
for(i = 0;i<4;i++){
printf("%d",r[i]);
}
/* for(digit=0;digit<4;digit++){
for(tmp=guess;tmp>0;tmp/=10){
if(tmp%10==digit){
printf("%d",digit);
g[i++]=digit;
}
}
}
printf("\n");
for(i=0;i<4;i++){
printf("%d",g[i]);
}//just to check
//this is for sorting the random
for(digit=0;digit<4;digit++){
for(tmp=random;tmp>0;tmp/=10){
if(tmp%10==digit){
printf("%d",digit);
r[i++]=digit;
}
}
}
for(i=0;i<4;i++){
printf("%d",r[i]);
}//just to check
*/
//this is for hints
rdig1=r[0];//redefining the random and guess digits so it is easier later
rdig2=r[1];
rdig3=r[2];
rdig4=r[3];
gdig1=g[0];
gdig2=g[1];
gdig3=g[2];
gdig4=g[3];
q = 0;
eq = 0;
lt = 0;
gt = 0;
if (random > 0){//loop that always holds true
if (gdig1 == rdig1){
eq++;
}else if (gdig1 < rdig1){
lt++;
}else if (gdig1 > rdig1){
gt++;
}
if (gdig2 == rdig2){
eq++;
}else if (gdig2 < rdig2){
lt++;
}else if (gdig2 > rdig2){
gt++;
}
if (gdig3 == rdig3){
eq++;
}else if (gdig3 < rdig3){
lt++;
}else if (gdig3 > rdig3){
gt++;
}
if (gdig4 == rdig4){
eq++;
}else if (gdig4 < rdig4){
lt++;
}else if (gdig4 > rdig4){
gt++;
}
}
for (q = 0; q < eq; q++){//counting step for the = <> no problems here^^
putchar('=');
}
for (q = 0; q < lt; q++){
putchar('<');
}
for (q = 0; q < gt; q++){
putchar('>');
}
//everything below is correct do not mess with *******************************************************************************************
} while (gdig1 > 0);//to loop inputs until they input correctly
if ((gdig1 == 0) || (gdig2 == 0) || (gdig3 == 0) || (gdig4 == 0)){//a nested if statement to check each digit if it is a 0
printf("\nYou have entered the Super Secret Code!!!!!\nThe actual passcode is:%d.\n",random);
break;
}
if ((rdig1 == gdig1) && (rdig2 == gdig2) && (rdig3 == gdig3) && (rdig4 == gdig4)){//to skip this codeblock and move onto next
break;
}
} while (((rdig1 != gdig1) && (rdig2 != gdig2) && (rdig3 != gdig3) && (rdig4 != gdig4)));//to loop inputs until they got it
//everything below is correct do not mess with *******************************************************************************************
if ((rdig1 == gdig1) && (rdig2 == gdig2) && (rdig3 == gdig3) && (rdig4 == gdig4)){
printf("\nCorrect Code inputted.");
sleep(1);
printf("\nImplementing unlocking procedures...\n");
for (i=0;i<=4;i++){//for fun cause why not. if you spend hours on code might as well have some fun :)
int p =25*i ;
printf("%d%% complete................\n",p);
sleep(1);
}
printf("Stand back!!! The vault is now opening.\n");
}
return 0;
}

Resources