Ok, I'm sorry in advance, I know there is a thousand examples of how do-while loops work and how to exit them. I swear I've tried them all. For the life of me, I cannot get this to exit when the user enters a 0. There has to be something I'm over-looking. How I got the one to work inside the else if loop but the main one is beyond me. I would appreciate any help or direction.
int main()
{
char connectBoard[6][7];
int i , j;
char c;
int turn = 1;
char player1 = 'x';
char player2 = 'o';
int spot;
for( i = 0; i < 6; i++) //sets up 2D array board
{
for( j = 0; j < 7; j++)
{
c = '.';
connectBoard[i][j] = c;
}
}
for( i = 0; i < 6; i++)// prints out 2D array board
{
for( j = 0; j < 7; j++)
{
printf("%c", connectBoard[i][j]);
}
printf("\n");
}
printf("=======\n");
printf("1234567\n");
do{
if( turn%2 != 0 ) //player1 turn
{
for( i = 5; i < 6; i++)
{
for(j = 0; j < 1; j++)
{
printf(" Player %c, drop your piece in which column (1-7 or 0 to quit): ", player1);
scanf("%d", &spot);
j = spot - 1;
if( connectBoard[i][j] == '.' )
{
connectBoard[i][j] = 'x';
}
else if( connectBoard[i][j] == 'x' || connectBoard[i][j] == 'o' )
{
do
{
i--;
}while(connectBoard[i][j] == 'x' || connectBoard[i][j] == 'o');
connectBoard[i][j] = 'x';
if( i == -1 )
{
printf("***Bad entry, try again: ");
scanf("%d", &spot);
j = spot - 1;
i = 5;
connectBoard[i][j] = 'x';
}
}
turn++;
}
break;
}
printf("\n");
for( i = 0; i < 6; i++)// prints out 2D array board
{
for( j = 0; j < 7; j++)
{
printf("%c", connectBoard[i][j]);
}
printf("\n");
}
printf("=======\n");
printf("1234567\n");
}
if( turn%2 == 0 ) //player2 turn
{
for( i = 5; i < 6; i++)
{
for(j = 0; j < 1; j++)
{
printf(" Player %c, drop your piece in which column (1-7 or 0 to quit): ", player2);
scanf("%d", &spot);
j = spot - 1;
if( connectBoard[i][j] == '.' )
{
connectBoard[i][j] = 'o';
}
else if( connectBoard[i][j] == 'x' || connectBoard[i][j] == 'o' )
{
do
{
i--;
}while(connectBoard[i][j] == 'x' || connectBoard[i][j] == 'o');
connectBoard[i][j] = 'o';
if( i == -1)
{
printf("***Bad entry, try again: ");
scanf("%d", &spot);
j = spot - 1;
i = 5;
connectBoard[i][j] = 'o';
}
}
turn++;
}
break;
}
printf("\n");
for( i = 0; i < 6; i++)// prints out 2D array board
{
for( j = 0; j < 7; j++)
{
printf("%c", connectBoard[i][j]);
}
printf("\n");
}
printf("=======\n");
printf("1234567\n");
}
}while( spot != 0 );
return 0;
}
After reading the spot variable, you can check if the value is 0 and break:
printf(" Player %c, drop your piece in which column (1-7 or 0 to quit): ", player2);
scanf("%d", &spot);
if (spot == 0)
return 1;
j = spot - 1;
and change your do while to a while loop to avoid confusion.
Use a variable only for that loop. You are using spot inside your loop to do other things, use it only for the while purpose.
Then, you know the diference between do/while and while? I dont understand very well your program, but if you are reading a spot, you can read it at beggining and outside the loop and then check it with a while loop. Its the same but maybe its more easy for you.
Why are you using spot for while loop. as per C language, It stores a garbage value.
Even if you use, please do not set values to it. Please check basic C variables declaration.
Related
For an Assignment I need to code the game Tic Tac Toe in C in Putty.
I cant find whats wrong with the program. No matter what I put as input the program returns "Player X Won!"
can anyone spot the issue?
heres the code for the functions
#include <stdio.h>
#include "tictac.h"
#define BOARD_SIZE 3 // size of the board
void print_theboard(char board[BOARD_SIZE][BOARD_SIZE]) {
for (int i = 0; i < BOARD_SIZE; i++) {
for (int j = 0; j < BOARD_SIZE; j++) {
printf(" %c ", board[i][j]);
if (j < BOARD_SIZE - 1) {
printf("|");
}
}
printf("\n");
if (i < BOARD_SIZE - 1) {
printf("---+---+---\n");
}
}
}
int check_whowon(char board[BOARD_SIZE][BOARD_SIZE]) {
//Gewinn in den Reihen
for (int i = 0; i < BOARD_SIZE; i++) {
if (board[i][0] == board[i][1] && board[i][1] == board[i][2])
return 1;
}
//Gewinn in den Spalten
for (int j = 0; j < BOARD_SIZE; j++) {
if (board[0][j] == board[1][j] && board[1][j] == board[2][j])
return 1;
}
//Gewinn in den Diagonalen
if (board[0][0] == board[1][1] && board[1][1] == board[2][2])
return 0;
if (board[0][2] == board[1][1] && board[1][1] == board[2][0])
return 1;
return 0;
}
~
heres the code for the .h file
void print_theboard();
int check_whowon();
int check_draw();
heres the code for the main
#include <stdio.h>
#include "tictac.h"
#define BOARD_SIZE 3 // size of the boad
int main() {
char board[BOARD_SIZE][BOARD_SIZE];
int row, col, game_over = 0;
char player = 'X';
// initialize the board with empty spaces
for (int i = 0; i < BOARD_SIZE; i++) {
for (int j = 0; j < BOARD_SIZE; j++) {
board[i][j] = ' ';
}
}
while (!game_over) {
// display the current status of the board
print_theboard(board);
printf("Player %c, enter the row and column (e.g. 0 2): ", player);
scanf("%d %d", &row, &col);
// validate the input
if (row >= 0 && row < BOARD_SIZE && col >= 0 && col < BOARD_SIZE) {
board[row][col] = player;
// check if the game is won or drawn
if (check_whowon(board)) {
printf("Player %c wins!\n", player);
game_over = 1;
}
else {
printf("Invalid input. Please try again.\n");
} if(player='X')
player='O';
else
player='X';
}
return 0;
}
}
No matter what I put as input the program returns "Player X Won!"
I guess that the check is weird. No matter where you place your token in the very first round, you will still have two rows which remain empty. Since you initialize your board with space chars, your row check
board[i][0] == board[i][1] && board[i][1] == board[i][2]
will evaluate to
' ' == ' ' && ' ' == ' '
which is true. You should check additionally if the row/column/diagonal actually has a token in it.
The board is initialized with spaces:
// initialize the board with empty spaces
for (int i = 0; i < BOARD_SIZE; i++) {
for (int j = 0; j < BOARD_SIZE; j++) {
board[i][j] = ' ';
}
}
The variable player is initialised with 'X' because X is playing first:
char player = 'X';
But when the check_whowon(board) function is called the first time, it checks if the horizontal, vertical or diagonal lines have the same character (not if they specifically have 'X' or 'O'). So it always returns true because of the initialised space characters.
To fix this, edit checkwhowon() function to ignore the space character or specifically check for 'X' and 'O'.
I am trying to fill a char matrix in C and print it in C but I get only weird characters. I am running this programm on windows, hence the system(cls);
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
void paint(char tab[10][10], int lignes, int colonnes)
{
system("cls");
for (int i = 0; i < lignes; i++)
{
for (int j = 0; j < colonnes; j++)
{
printf("%c", tab[i][j]);
}
printf("\n");
}
}
void create(char tab[10][10], int lignes, int colonnes)
{
for (int i = 0; i < lignes; i++)
{
for (int j = 0; j < colonnes; j++)
{
tab[i][j] = ' ';
if (i == 0 || i == lignes--)
tab[i][j] = 205;
if (j == 0 || j == colonnes--)
tab[i][j] = 186;
if (i == 0 && j == 0)
tab[i][j] = 201;
if (i == 0 && j == colonnes--)
tab[i][j] = 187;
if (i == lignes-- && j == 0)
tab[i][j] = 200;
if (i == lignes-- && j == colonnes--)
tab[i][j] = 188;
if (i == 50 && j == 50)
tab[i][j] = 248;
}
}
}
int main()
{
char tab[10][10];
create(tab, 10, 10);
paint(tab, 10, 10);
char i;
while(scanf(" %c", &i) != 'q')
{
}
}
I tried changing the output type in printf with %d and %s as shown in other answers here, but %d shows random numbers and %s makes the programm crash, I don't know if it is because of a segfault somewhere.I also tried using simple characters to fill my matrix, not their ascii value.
Don't mind the ineffective scanf , I am still trying to figure out keyboard input without using enter on my own for now.
there were a few problems with your code, like colonnes-- changes the value of colonnes while cheking your if statement
I believe you wanted to draw a rectangle
try this (it's your own code, modified a bit)
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
char tab[10][10];
void paint(int lignes, int colonnes)
{
system("cls");
for (int i = 0; i < lignes; i++)
{
for (int j = 0; j < colonnes; j++)
{
printf("%c", tab[i][j]);
}
printf("\n");
}
}
void create(int lignes, int colonnes)
{
for (int i = 0; i < lignes; i++)
{
for (int j = 0; j < colonnes; j++)
{
tab[i][j] = ' ';
if (i == 0 || i == lignes-1)
tab[i][j] = 205;
if (j == 0 || j == colonnes-1)
tab[i][j] = 186;
if (i == 0 && j == 0)
tab[i][j] = 201;
if (i == 0 && j == colonnes-1)
tab[i][j] = 187;
if (i == lignes-1 && j == 0)
tab[i][j] = 200;
if (i == lignes-1 && j == colonnes-1)
tab[i][j] = 188;
if (i == 50 && j == 50)
tab[i][j] = 248;
}
}
}
int main()
{
create(10, 10);
paint(10, 10);
}
I am in the process of creating hangman in C language, but there is one problem that I cannot quite grasp. When a user correctly guesses one of the letters that the word that is being guessed has, the program replaces all of previously guessed letters to the one user just put. What is the source of this problem?
#include<stdio.h>
#include <stdlib.h>
int main()
{
srand(time(NULL));
int x = 0, isCompleted, matchFound, numberOfTries = 7;
char letterGuess[1];
int randomIndex = rand()%14;
const char *wordArray[14];
const char *guessedWord[10];
const char *usedLetters[17];
for (int k = 0; k < 10; k++) {
guessedWord[k] = "_";
}
wordArray[0] = "butonierka";
wordArray[1] = "centyfolia";
wordArray[2] = "chiroplast";
wordArray[3] = "cmentarzyk";
wordArray[4] = "chrustniak";
wordArray[5] = "budowniczy";
wordArray[6] = "cholewkarz";
wordArray[7] = "cornflakes";
wordArray[8] = "brzydactwo";
wordArray[9] = "germanofil";
wordArray[10] = "lichtarzyk";
wordArray[11] = "lutowniczy";
wordArray[12] = "mikrocysta";
wordArray[13] = "tryskawiec";
const char *wordToGuess = wordArray[randomIndex];
for(int i = 0; i < 10; i++) {
printf(" %s ", guessedWord[i]);
}
printf("\n");
while(numberOfTries != 0 && isCompleted != 10) {
matchFound = 0;
isCompleted = 0;
printf("Please give a lowercase letter\n");
printf("Left tries: %d\n", numberOfTries);
scanf("%s", &letterGuess);
for (int z = 0; z < 17; z++) {
if (usedLetters[z] == letterGuess[0]) {
matchFound = 1;
}
}
if (letterGuess[0] >= 'a' && letterGuess[0] <= 'z' && matchFound == 0) {
usedLetters[x] = letterGuess[0];
x++;
for (int j = 0; j < 10; j++) {
if (letterGuess[0] == wordArray[randomIndex][j])
guessedWord[j] = letterGuess;
matchFound = 1;
}
}
if (matchFound == 0) {
numberOfTries--;
}
for(int z = 0; z < 10; z++) {
printf(" %s ", guessedWord[z]);
}
printf("\n");
} else {
if (matchFound == 1) {
printf("You've already given such letter!!\n");
} else {
printf("Wrong input, please try again!\n");
}
}
for (int k = 0; k < 10; k++) {
if (guessedWord[k] != "_") {
isCompleted++;
}
}
if (isCompleted == 10) {
printf("You have correctly guessed a word! Congrats!!\n");
}
printf("\n\n");
}
printf("The word was: %s\n", wordArray[randomIndex]);
printf("Game over!!\n");
}
The problem is that you're storing letterGuess, rather than individual characters. So each time letterGuess is updated with a new guess, all references to it change. Also, letterGuess is too short, leaving no room for the terminating null character.
The best solution is to make letterGuess a char (or an int), not an array, and to make guessedWord a char [] rather than a char *[]. There is no reason to use strings for single characters. That will solve the string-sharing problem.
I had my previous question closed as it was too broad, so I decided to write code in parts and only ask something if I had a specific problem.
My code so far sees what parts of chess board are filled with knights (in code 'H') and fill the parts they dominate with 'T'. The goal is to fill all parts of the chess board with 'H' and 'T' and to have not dominated blocks left at '0'. Then the program tests if all the fields are either 'T' or 'H', if yes returns 1, if no 0. Then it prints out the chess board.
However it only prints out 7 rows (still 8 columns though), and even though I filled the board with 12 knights that are in position to dominate the entire board, somehow there are still 0s left.
Here is the code:
#include <stdio.h>
#include <stdlib.h>
int filling(char array[7][7])
{
int i, j;
for (i = 0;i < 8;i++)
{
for (j = 0; j < 8;j++)
{
if(array[i][j] == 'H')
{
if(i-1>-1 && j+2<8) array[i-1][j+2]='T';
if(i+1<8 && j+2<8) array[i+1][j+2]='T';
if(i-2>-1 && j+1<8) array[i-2][j+1]='T';
if(i+2<8 && j+1<8) array[i+2][j+1]='T';
if(i-2>-1 && j-1>-1) array[i-2][j-1]='T';
if(i+2<8 && j-1>-1) array[i+2][j-1]='T';
if(i-1>-1 && j-2>-1) array[i-1][j-2]='T';
if(i+1<8 && j-2>-1) array[i+1][j-2]='T';
}
}
}
}
int checking(char array[7][7])
{
int i, j;
for(i = 0;i < 8;i++)
{
for(j = 0;j < 8;j++)
{
if(array[i][j] != 'H' && array[i][j] != 'T')
return 0;
else return 1;
}
}
}
int main()
{
int i, j;
char board[7][7];
for(i = 0; i < 8; i++)
{
for(j = 0; j < 8; j++)
board[i][j] = '0';
}
board[2][1] = 'H';
board[2][2] = 'H';
board[3][2] = 'H';
board[5][2] = 'H';
board[6][2] = 'H';
board[5][3] = 'H';
board[2][4] = 'H';
board[1][5] = 'H';
board[2][5] = 'H';
board[4][5] = 'H';
board[5][5] = 'H';
board[5][6] = 'H';
filling(board);
if(checking(board) == 1) printf ("Works");
else printf ("Doesnt work");
for(i = 0; i < 8; i++)
{
printf("\n");
for(j = 0; j < 8; j++)
printf("%c ", board[i][j]);
}
return 0;
}
What do you think is the problem here? Am I doing something worng?
Thank you for your answers.
EDIT: Made a mistake not making sure that horses dont get changed into 'T's. Here is the fixed code:
#include <stdio.h>
#include <stdlib.h>
int pildymas(char array[7][7])
{
int i, j;
for (i = 0;i < 8;i++)
{
for (j = 0; j < 8;j++)
{
if(array[i][j] == 'H')
{
if(i-1>-1 && j+2<8 && array[i-1][j+2]!='H') array[i-1][j+2]='T';
if(i+1<8 && j+2<8 && array[i+1][j+2]!='H') array[i+1][j+2]='T';
if(i-2>-1 && j+1<8 && array[i-2][j+1]!='H') array[i-2][j+1]='T';
if(i+2<8 && j+1<8 && array[i+2][j+1]!='H') array[i+2][j+1]='T';
if(i-2>-1 && j-1>-1 && array[i-2][j-1]!='H') array[i-2][j-1]='T';
if(i+2<8 && j-1>-1 && array[i+2][j-1]!='H') array[i+2][j-1]='T';
if(i-1>-1 && j-2>-1 && array[i-1][j-2]!='H') array[i-1][j-2]='T';
if(i+1<8 && j-2>-1 && array[i+1][j-2]!='H') array[i+1][j-2]='T';
}
}
}
}
int tikrinimas(char array[7][7])
{
int i, j;
for(i = 0;i < 8;i++)
{
for(j = 0;j < 8;j++)
{
if(array[i][j] != 'H' && array[i][j] != 'T')
return 0;
else return 1;
}
}
}
int main()
{
int i, j;
char board[7][7];
for(i = 0; i < 8; i++)
{
for(j = 0; j < 8; j++)
board[i][j] = '0';
}
board[2][1] = 'H';
board[2][2] = 'H';
board[3][2] = 'H';
board[5][2] = 'H';
board[6][2] = 'H';
board[5][3] = 'H';
board[2][4] = 'H';
board[1][5] = 'H';
board[2][5] = 'H';
board[4][5] = 'H';
board[5][5] = 'H';
board[5][6] = 'H';
for(i = 0; i < 8; i++)
{
printf("\n");
for(j = 0; j < 8; j++)
printf("%c ", board[i][j]);
}
pildymas(board);
if(tikrinimas(board) == 1) printf ("Veikia");
else printf ("neveikia");
for(i = 0; i < 8; i++)
{
printf("\n");
for(j = 0; j < 8; j++)
printf("%c ", board[i][j]);
}
return 0;
}
I notice people say that my array doesnt have enough size. I was under impression that an array A[2] will have A[0] A[1] and A[2] spots, is this wrong?
Thank you for your help, I am pretty sure that I will be back tonight though :)
EXTRA QUESTION
I have it all working now with filling and testing, but I need to find a way to fill the board with all the possible combinations of 12 horses, and the only way I have in mind is creating a for cycle that is 12 levels deep, meaning it would have to do 64^12 cycles. Is there any other way I could try every possible positioning of 12 horses on a 8x8 board? My plan is to take a position, then test if it fits, if so save it, and then try another one until all the positions are done. As far as I know there are only 1\2 combinations of 12 horses to dominate all fields on the board.
I have no idea what type of dominance you are attempting to create on the board, but to get your indexes within the correct bounds for a 7x7 array, you will need something similar to the following:
#include <stdio.h>
enum { DIM = 7 };
void filling (char (*array)[DIM])
{
int i, j;
for (i = 0; i < DIM; i++)
for (j = 0; j < DIM; j++)
if (array[i][j] != 'H')
array[i][j] = 'T';
}
int checking (char (*array)[DIM])
{
int i, j;
for (i = 0; i < DIM; i++)
for (j = 0; j < DIM; j++)
if (array[i][j] != 'H' && array[i][j] != 'T')
return 0;
return 1;
}
int main (void) {
int i, j;
char board[DIM][DIM] = {{0}};
board[2][1] = 'H';
board[2][2] = 'H';
board[3][2] = 'H';
board[5][2] = 'H';
board[6][2] = 'H';
board[5][3] = 'H';
board[2][4] = 'H';
board[1][5] = 'H';
board[2][5] = 'H';
board[4][5] = 'H';
board[5][5] = 'H';
board[5][6] = 'H';
filling (board);
if (checking (board) == 1)
printf ("Works");
else
printf ("Doesnt work");
for (i = 0; i < DIM; i++) {
printf ("\n");
for (j = 0; j < DIM; j++)
printf ("%c ", board[i][j]);
}
putchar ('\n');
return 0;
}
Example Use/Output
$ ./bin/chess
Works
T T T T T T T
T T T T T H T
T H H T H H T
T T H T T T T
T T T T T H T
T T H H T H H
T T H T T T T
Or the beauty in properly eliminating all magic numbers from your code and using proper constants is changing 1 number is all that is needed to create an 8x8. e.g.
enum { DIM = 8 };
And now the output is:
$ ./bin/chess
Works
T T T T T T T T
T T T T T H T T
T H H T H H T T
T T H T T T T T
T T T T T H T T
T T H H T H H T
T T H T T T T T
T T T T T T T T
Code with everything fixed and working.
#include <stdio.h>
#include <stdlib.h>
int pildymas(char array[8][8])
{
int i, j;
for (i = 0;i < 8;i++)
{
for (j = 0; j < 8;j++)
{
if(array[i][j] == 'H')
{
if(i-1>-1 && j+2<8 && array[i-1][j+2]!='H') array[i-1][j+2]='T';
if(i+1<8 && j+2<8 && array[i+1][j+2]!='H') array[i+1][j+2]='T';
if(i-2>-1 && j+1<8 && array[i-2][j+1]!='H') array[i-2][j+1]='T';
if(i+2<8 && j+1<8 && array[i+2][j+1]!='H') array[i+2][j+1]='T';
if(i-2>-1 && j-1>-1 && array[i-2][j-1]!='H') array[i-2][j-1]='T';
if(i+2<8 && j-1>-1 && array[i+2][j-1]!='H') array[i+2][j-1]='T';
if(i-1>-1 && j-2>-1 && array[i-1][j-2]!='H') array[i-1][j-2]='T';
if(i+1<8 && j-2>-1 && array[i+1][j-2]!='H') array[i+1][j-2]='T';
}
}
}
}
int tikrinimas(char array[8][8])
{
int i, j;
for(i = 0;i < 8;i++)
{
for(j = 0;j < 8;j++)
{
if(array[i][j] != 'H' && array[i][j] != 'T')
return 0;
}
}
return 1;
}
int main()
{
int i, j;
char board[8][8];
for(i = 0; i < 8; i++)
{
for(j = 0; j < 8; j++)
board[i][j] = '0';
}
board[2][1] = 'H';
board[2][2] = 'H';
board[3][2] = 'H';
board[5][2] = 'H';
board[6][2] = 'H';
board[5][3] = 'H';
board[2][4] = 'H';
board[1][5] = 'H';
board[2][5] = 'H';
board[4][5] = 'H';
board[5][5] = 'H';
board[5][6] = 'H';
for(i = 0; i < 8; i++)
{
printf("\n");
for(j = 0; j < 8; j++)
printf("%c ", board[i][j]);
}
pildymas(board);
if(tikrinimas(board) == 1) printf (" \n Veikia");
else printf ("\n neveikia");
for(i = 0; i < 8; i++)
{
printf("\n");
for(j = 0; j < 8; j++)
printf("%c ", board[i][j]);
}
return 0;
}
Solved my problem. Here are the things I had to change:
1) Increase array size by 1 in both dimensions.
2) Make sure my 'H's dont get overwritten by 'T's.
3) In checking function I made a mistake of including return 1 in a cycle, which therefore would print out that it works as long as the first value was working. I left return 0 in cycle, because as long as there is one spot empty, it doesnt work, but I moved return 1 after the cycle, as if only there are no empty spaces it should count as a success.
This is the code of a simple code of game of anagram:
#define NP 5
#define LP 10
void anagrammi() {
srand(time (NULL));
char words[NP][LP] = {"cane", "gatto", "gallo", "rana", "ibis"};
typedef struct {
char parola[LP];
int usati[LP];
char anagramma[LP];
}an ;
int a, len, i, j, cont;
char merda[LP], c;
an yoda;
a = rand()%5;
strcpy(yoda.parola, words[a]);
len = strlen(yoda.parola);
for (i = 0; i < len; i++){
yoda.usati[i] = len+2;
}
i = 0;
while (i < len){
cont = 0;
a = rand()%len;
for (j = 0; j < len; j++){
if (a == yoda.usati[j]) cont++;
} if (cont == 0) {
yoda.usati[i] = a;
yoda.anagramma[i] = yoda.parola[a];
i++;
}
}
j = 0;
cont = 0;
yoda.anagramma[i] = '\0';
printf("Anagramma di %s ? ", yoda.anagramma);
c = getchar() ;
while( c != '\n' && j < LP ){
merda[j] = c ;
j++ ;
c = getchar(); }
merda[j] = '\0';
for (i = 0; i < len; i++){
if (merda[i] =! yoda.parola[i]) cont++;
printf("\n%d", cont);
printf("\n%c", merda[i]);
printf("\n%c", yoda.parola[i]);
}
if (cont =! 0) printf("\nFALSE");
else printf ("\nTRUE");
}
The program goes, but when I insert the word and compare this to the original word, the final output is always FALSE, even if I put the right word. The problem occurs also without \0.Can anyone give me a hand?
Thanks to the answer, this is the perfectly working programm:
void anagrammi() {
srand(time (NULL));
char words[NP][LP] = {"cane", "gatto", "gallo", "rana", "ibis"};
typedef struct {
char parola[LP];
int usati[LP];
char anagramma[LP];
}an ;
int a, len, i, j, cont;
char merda[LP], c;
an yoda;
a = rand()%5;
strcpy(yoda.parola, words[a]);
len = strlen(yoda.parola);
for (i = 0; i < len; i++){
yoda.usati[i] = len+2;
}
i = 0;
while (i < len){
cont = 0;
a = rand()%len;
for (j = 0; j < len; j++){
if (a == yoda.usati[j]) cont++;
} if (cont == 0) {
yoda.usati[i] = a;
yoda.anagramma[i] = yoda.parola[a];
i++;
}
}
j = 0;
cont = 0;
yoda.anagramma[i] = '\0';
printf("Anagramma di %s ? ", yoda.anagramma);
c = getchar() ;
while( c != '\n' && j < LP ){
merda[j] = c ;
j++ ;
c = getchar(); }
for (i = 0; i < len; i++){
if (merda[i] != yoda.parola[i]) cont++;
}
i = strlen(merda);
if (cont != 0 || i > len ) printf("\nNON HAI INDOVINATO");
else printf ("\nHAI INDOVINATO");
}
(cont =! 0) that should be (cont != 0)
– kaylum