Why does my programm call the while loop just once ? (Atmel C) - c

Please help. My program runs the while loop just once :(
I don't know what the problem is. I programmed the same thing as well in python and there it worked fine.
I am a beginner in C and I am using the AtMega8 Microprocessor from Atmel.
#define F_CPU 8000000UL
#include <avr/io.h>
#include <util/delay.h>
int main(void) {
DDRD = 0xFF;
int world[9];
int nextworld[9];
//from 1 to 8
//some random start values
world[1] = 1;
world[2] = 1;
world[3] = 1;
world[6] = 1;
world[7] = 1;
world[8] = 1;
for (int i = 0; i < 9; i++) {
nextworld[i] = world[i];
}
int binworld = 0;
int tiles = 0;
//=============================
if (world[1] == 1) {
binworld = binworld + 128;
}
if (world[2] == 1) {
binworld = binworld + 64;
}
if (world[3] == 1) {
binworld = binworld + 32;
}
if (world[4] == 1) {
binworld = binworld + 16;
}
if (world[5] == 1) {
binworld = binworld + 8;
}
if (world[6] == 1) {
binworld = binworld + 4;
}
if (world[7] == 1) {
binworld = binworld + 2;
}
if (world[8] == 1) {
binworld = binworld + 1;
}
PORTD = binworld;
//================================
while (1) {
_delay_ms(100);
tiles = 0;
//the live starts
for (int i = 0; i < 9; i++) {
tiles = 0;
for (int xc = -1; xc <= 1; xc++) {
if (world[i + xc] == 1) {
tiles += 1;
}
if (world[i] == 1) {
tiles = tiles - 1;
}
}
if (world[i] == 1 && tiles == 0) {
nextworld[i] = 0;
}
else if (world[i] == 0 && tiles == 1) {
nextworld[i] = 1;
}
else if (world[i] == 1 && tiles == 2) {
nextworld[i] = 0;
}
}
//update old world
for (int i = 0; i < 9; i++) {
world[i] = nextworld[i];
}
//set null
binworld = 0;
//convert
if (world[1] == 1) {
binworld = binworld + 128;
}
if (world[2] == 1) {
binworld = binworld + 64;
}
if (world[3] == 1) {
binworld = binworld + 32;
}
if (world[4] == 1) {
binworld = binworld + 16;
}
if (world[5] == 1) {
binworld = binworld + 8;
}
if (world[6] == 1) {
binworld = binworld + 4;
}
if (world[7] == 1) {
binworld = binworld + 2;
}
if (world[8] == 1) {
binworld = binworld + 1;
}
PORTD = binworld;
}
}

Maybe because the array index is negative on the first iteration
for (int i=0; i<9;i++)
{
tiles = 0;
for (int xc=-1; xc <= 1; xc++)
{
if (world[i+xc]==1) <== HERE, i(0) + xc(-1) == -1

Related

For Loop running infinite C

I am making a checksum error verifying program using C language but I have encountered an infinite loop while executing the program and I don't have any clue why I am getting this.
So what I have done in this program is that I am going to first convert characters of a string into respective ASCII codes and then checking for checksum error and for this I have made this program below.
#include <stdio.h>
int a[2], i, dec[10][8], de[2][8], j, k, add[8], carry = 0, n = 5;
int u[] = {0, 0, 0, 0, 0, 0, 0, 1};
char b[] = {'H', 'e', 'l', 'l', 'o'};
int main()
{
for (k = 0; k < 2; k++)
{
for (j = 0; j < 8; j++)
{
dec[k][j] = 0;
}
}
for (k = 0; k < 2; k++)
{
a[k] = b[k];
}
for (k = 0; k < 2; k++)
{
i = 0;
while (a[k] > 0)
{
dec[k][i] = a[k] % 2;
a[k] = a[k] / 2;
i++;
}
}
i = 0;
s:
for (k = 0; k < 2; k++)
{
for (j = 7; j >= 0; j--)
{
de[k][i] = dec[k][j];
i++;
}
i = 0;
}
for (k = 0; k < 2; k++)
{
for (j = 0; j < 8; j++)
{
printf("%d", de[k][j]);
}
printf("\n");
}
i = 0;
for (j = 7; j >= 0; j--)
{
if (de[i][j] == 1 && de[i + 1][j] == 1 && carry == 0)
{
add[j] = 0;
carry = 1;
}
else if (de[i][j] == 1 && de[i + 1][j] == 1 && carry == 1)
{
add[j] = 1;
carry = 1;
}
else if (de[i][j] == 0 && de[i + 1][j] == 1 && carry == 1)
{
add[j] = 0;
carry = 1;
}
else if (de[i][j] == 0 && de[i + 1][j] == 1 && carry == 0)
{
add[j] = 1;
carry = 0;
}
else if (de[i][j] == 0 && de[i + 1][j] == 1 && carry == 1)
{
add[j] = 0;
carry = 1;
}
else if (de[i + 1][j] == 0 && de[i][j] == 1 && carry == 0)
{
add[j] = 1;
carry = 0;
}
else if (de[i + 1][j] == 0 && de[i][j] == 0 && carry == 0)
{
add[j] = 0;
carry = 0;
}
else if (de[i + 1][j] == 0 && de[i][j] == 0 && carry == 1)
{
add[j] = 1;
carry = 0;
}
}
i = i + 2;
for (k = 0; k < 8; k++)
{
de[i][j] = add[j];
}
if (carry == 1 && i < n)
{
for (k = 0; k < 8; k++)
{
de[i + 1][j] = u[j];
}
goto s;
}
else if (carry == 0 && i < n)
{
for (k = 0; k < 8; k++)
{
de[i + 1][j] = u[j];
}
goto s;
}
for (i = 0; i < 8; i++)
printf("%d", add[i]);
return 0;
}

Get Key State in linux C

OK ive searched about for quite some time now but i simply cannot find a replacement for the GetKeyState() function in linux. All i need and want is to simply poll the arrow keys, and if they are pressed, execute something. My home PC is linux based and my students' PC is windows based, so when i worked in that, i wrote this code:
#include <stdio.h>
#include <time.h>
#include <stdlib.h>
#include <dos.h>
#include <windows.h>
int x; int y, redraw;
int i; int l, xm, ym, op, as, b, ab;
short int display[10][10];
void draw() {
if (redraw == 1) {
system("cls");
while (l < 10) {
while (i<10) {
if (display[i][l] == 0) { printf("="); }
if (display[i][l] == 1) { printf("X"); }
if (display[i][l] == 2) { printf("w"); }
if (display[i][l] == 3) { printf("0"); }
if (display[i][l] == 4) { printf("#"); }
if (display[i][l] == 5) { printf("M"); }
if (display[i][l] == 6) { printf("H"); }
if (display[i][l] == 7) { printf("8"); }
printf("|");
i++;
}
i = 0;
printf("\n");
printf("-+-+-+-+-+-+-+-+-+-+");
printf("\n");
l++;
}
l = 0;
redraw = 0;
}
}
void getkeys() {
while (b == 0) {
if (GetKeyState(VK_LEFT) & 0x8000)
{
xm = -1;
b = 1;
}
if (GetKeyState(VK_RIGHT) & 0x8000)
{
xm = 1;
b = 1;
}
if (GetKeyState(VK_UP) & 0x8000)
{
ym = -1;
b = 1;
}
if (GetKeyState(VK_DOWN) & 0x8000)
{
ym = 1;
b = 1;
}
if (GetKeyState(VK_BACK) & 0x8000)
{
op = -1;
b = 1;
}
if (GetKeyState(VK_RETURN) & 0x8000)
{
op = 1;
b = 1;
}
} b = 0; redraw = 1;
}
void cursor() {
display[x][y] = as;
x = x + xm;
xm = 0;
y = y + ym;
ym = 0;
if (x >9) { x = 0; }
if (y >9) { y = 0; }
if (x <0) { x = 9; }
if (y <0) { y = 9; }
ab = display[x][y];
as = ab;
if (as == 0) {
display[x][y] = 4;
}
if (as == 1) {
display[x][y] = 5;
}
if (as == 2) {
display[x][y] = 6;
}
if (as == 3) {
display[x][y] = 7;
}
Sleep(100);
}
void main()
{
while (i < 10) {
while (l<10) { display[l][i] = rand() % 4; l++; } l = 0; i++;
}
redraw = 1;
while (1) {
draw();
getkeys();
b = 0;
cursor();
}
}
now this basically prints an array and a cursor on it, but it does use the GetKeyState() function and i just can not find an alternative to it on linux. So is there any simple alternative to the mentioned function and is it possible to make the source code multiplatform somehow? Thanks in advance.

loop not completing its iteration in C

I am however having trouble with my game loop! Currently when an invalid move is made, the loop iterates correctly. However when a valid move is made, my program displays the changed board, however appears to freeze in the middle of the loop! i have the location where it freezes (Test Spot 2 in the code), but i have no clue why. The desired output of my code is to keep on iterating until there are no moves available. (I haven't created the check for no moves yet as I cannot get my code to run in a normal counted loop!)
The code working when it is Invalid and not when it is valid is shown below.
here
Main Function where the issue occurs
int main(void)
{
int n;
int p;
char oppChar;
char playChar;
int i;
int pTurn;
int deltaRow;
int deltaCol;
int endGame = 999;
printf("Enter the board dimension: ");
fflush(stdout);
scanf("%d", &n);
printf("Computer plays (B/W) ");
fflush(stdout);
scanf(" %c", &oppChar);
if (oppChar == 'B') {
pTurn = 0;
playChar = 'W';
} else {
pTurn = 1;
playChar = 'B';
}
char board[n][26];
int movesAvalB[n][26];
int movesAvalW[n][26];
int AIBoard[n][26];
for (i = 0; i < n; i++) {
for (p = 0; p < n; p++) {
board[i][p] = 'U';
}
}
board[(n / 2) - 1][(n / 2) - 1] = 'W';
board[(n / 2) - 1][(n / 2)] = 'B';
board[(n / 2)][(n / 2) - 1] = 'B';
board[n / 2][n / 2] = 'W';
printBoard(board, n);
int q = 0;
do {
q++;
// testing
printf(" \n");
printf("Turn Number %d \n", pTurn);
// variable decleration
int k = 0;
int y = 0;
int x = 0;
int max = 0;
int temp = 0;
char c[3] = " ";
// arrays are all 0
for (i = 0; i < n; i++) {
for (p = 0; p < n; p++) {
AIBoard[i][p] = 0;
movesAvalB[i][p] = 0;
movesAvalW[i][p] = 0;
}
}
// moves avaliable to W
for (y = 0; y < n; y++) {
for (x = 0; x < n; x++) {
if (moves(n, x, y, board, 'W')) {
movesAvalW[y][x] = 1;
}
}
}
printf("Test Spot 1\n");
// moves avaliable to B
for (y = 0; y < n; y++) {
for (x = 0; x < n; x++) {
if (moves(n, x, y, board, 'B')) {
printf("Test Spot 1.5\n");
movesAvalB[y][x] = 1;
}
}
}
fflush(stdout);
printf("Test Spot 2\n");
// pTurn = pTurn%2;
fflush(stdout);
printf("Enter a move for colour %c (RowCol): \n", playChar);
fflush(stdout);
scanf("%s", &c);
if (positionInBounds(n, c[0], c[1])) {
int y = c[0] - 97;
int x = c[1] - 97;
if (playChar == 'B') {
if (movesAvalB[y][x] == 1) {
for (deltaRow = -1; deltaRow <= 1; deltaRow++) {
for (deltaCol = -1; deltaCol <= 1; deltaCol++) {
if (positionIntBounds(n, (x + deltaRow), (y + deltaCol))) {
i = 1;
while ((positionIntBounds(n, (y + (i * deltaRow)), (x + (i * deltaCol)))) &&
(board[y + (i * deltaRow)][x + (i * deltaCol)] == 'W')) {
i++;
if ((positionIntBounds(n, (y + (i * deltaRow)), (x + (i * deltaCol)))) &&
(board[y + (i * deltaRow)][x + (i * deltaCol)] == 'B')) {
while (i != 0) {
i--;
board[y + (i * deltaRow)][x + (i * deltaCol)] = 'B';
}
}
}
}
}
}
for (deltaRow = -1; deltaRow <= 1; deltaRow++) {
for (deltaCol = -1; deltaCol <= 1; deltaCol++) {
if (board[y + deltaRow][x + deltaCol] == 'W') {
board[y + deltaRow][x + deltaCol] == 'B';
}
}
}
printBoard(board, n);
} else {
printf("Invalid Move.");
}
}
if (playChar == 'W') {
if (movesAvalW[y][x] == 1) {
for (deltaRow = -1; deltaRow <= 1; deltaRow++) {
for (deltaCol = -1; deltaCol <= 1; deltaCol++) {
if (positionIntBounds(n, (x + deltaRow), (y + deltaCol))) {
i = 1;
while ((positionIntBounds(n, (y + (i * deltaRow)), (x + (i * deltaCol)))) &&
(board[y + (i * deltaRow)][x + (i * deltaCol)] == 'B')) {
i++;
if ((positionIntBounds(n, (y + (i * deltaRow)), (x + (i * deltaCol)))) &&
(board[y + (i * deltaRow)][x + (i * deltaCol)] == 'W')) {
while (i != 0) {
i--;
board[y + (i * deltaRow)][x + (i * deltaCol)] = 'W';
}
}
}
}
}
}
for (deltaRow = -1; deltaRow <= 1; deltaRow++) {
for (deltaCol = -1; deltaCol <= 1; deltaCol++) {
if (board[y + deltaRow][x + deltaCol] == 'B') {
board[y + deltaRow][x + deltaCol] == 'W';
}
}
}
printBoard(board, n);
} else {
printf("Invalid Move.");
}
}
} else {
printf("Invalid Move.");
}
pTurn++;
} while (q <= 5);
printf("were out");
}
Secondary Functions which you probably don't need but maybe
#include <stdio.h>
#include <string.h>
#include <stdbool.h>
void printBoard(char board[][26], int n)
{
int i;
char alpha[27] = "abcdefghijklmnopqrstuvwxyz";
printf(" ");
for (i = 0; i < n; i++) {
printf("%c", alpha[i]);
}
int p;
int q;
for (p = 0; p < n; p++) {
printf("\n");
printf("%c", alpha[p]);
for (q = 0; q < n; q++) {
printf("%c", board[p][q]);
}
}
}
bool positionInBounds(int n, char row, char col)
{
int p = row - 97;
int d = col - 97;
if (p > n) {
return false;
}
if (d > n) {
return false;
}
if (0 > p) {
return false;
}
if (0 > d) {
return false;
}
return true;
}
bool positionIntBounds(int n, int row, int col)
{
if (row > n) {
return false;
}
if (col > n) {
return false;
}
if (0 > row) {
return false;
}
if (0 > col) {
return false;
}
return true;
}
bool checkLegalInDirection(char board[][26], int n, char row, char col, char colour, int deltaRow, int deltaCol)
{
int i = 0;
while ((positionIntBounds(n, (row + (i * deltaRow)), (col + (i * deltaCol)))) &&
(board[row + (i * deltaRow)][col + (i * deltaCol)] != colour) &&
(board[row + (i * deltaRow)][col + (i * deltaCol)] != 'U')) {
i++;
if ((positionIntBounds(n, (row + (i * deltaRow)), (col + (i * deltaCol)))) &&
(board[row + (i * deltaRow)][col + (i * deltaCol)] == colour)) {
return true;
}
}
return false;
}
bool moves(int n, int x, int y, char board[][26], char colour)
{
int deltaRow;
int deltaCol;
if (board[y][x] == 'U') {
for (deltaRow = -1; deltaRow <= 1; deltaRow++) {
for (deltaCol = -1; deltaCol <= 1; deltaCol++) {
if (positionIntBounds(n, (x + deltaRow), (y + deltaCol))) {
if (checkLegalInDirection(board, n, (x + deltaRow), (y + deltaCol), colour, deltaRow, deltaCol)) {
return true;
}
}
}
}
}
return false;
}
(I was told to put fflush(stdout) before my scanf statements but this didnt fix the problem)

C algorithm to try out all the possible combinations of 12 knights in a chess board

I have been writing this program that tries to find how to put 12 knights on a chess board so all squares are either taken up by a knight or one of 12 knights can reach them in one move. So far I have created 2 functions: one takes a board with 12 knights and fills all the squares they can reach with 'T' and another that takes a filled chess board and checks if there are left any squares not taken up by a knight nor dominated by it (meaning no knight can reach it in one move).
Now I am dealing with the problem of how can I try out all the possible combinations of knights. My idea is that after every single combination of 12 knights on a board, I will send the board with 12 knights to be filled with 'T's for all the squares they can reach, and then send it to another function to check if all squares are dominated.
Here is my code:
#include <stdio.h>
#include <stdlib.h>
int filling(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 checking(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';
}
// The following lines are here to test if the checking and filling work
/*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 (" \n Works");
else printf ("\n Doesnt work");
for(i = 0; i < 8; i++)
{
printf("\n");
for(j = 0; j < 8; j++)
printf("%c ", board[i][j]);
}
return 0;
}
What kind of algorithm I could use to try out every combo? Thank you for your answers.
You need these things:
A sorting rule that puts all possible combinations in a well-defined order.
An initialization rule that defines the board's first such combination.
An increment rule that transitions a board from its current combination to the next combination in the well-defined order.
A rule that detects when the board is in the last combination.
Then you just use algorithm 2 to put the board in the first state. Then check with algorithm 4 to see if you're in the last state. If not, use algorithm 3 to go to the next state.
Algorithm 1 is probably the hard one. One simple rule is just to convert the board's position to a binary number with a zero for an empty square and a one for a full square in a well-defined order, say starting from the top left and going across, then moving to the next row.
If you're on decent, or even semi-decent hardware, you've got a 64-bit unsigned integral type available to you. Let's call that uint64_t.
You can store a chessboard with knight positions as a single uint64_t. You can also store a "dominated" mask in the same type. You simply establish a correlation between the 64 bits in the type and the 64 spaces on the chessboard.
Since there are 64 possible locations, you can pre-compute the possible threat masks for each position, storing that as an array of 64 uint64_t values.
You need to set the position of each knight. But you can safely do them in a certain order, so that knight #1 is always the "highest" knight - his bit position is always highest, or lowest, or whatever. So you could write code like this:
for (k0 = 64; k0 > 11; --k0)
for (k1 = k0 - 1; k1 > 10; --k1)
for (k2 = k1 - 1; k2 > 9; --k2)
...
for (k11 = k10 - 1; k11 >= 0; --k11)
/* do stuff */
But that's horrible, because there are a squillion possibilities (squillion = 1573144097507348889600, thanks #jwd!).
That said, the threat mask for each knight can be computed "incrementally" from the masks of the outer knights, so it might be faster to perform all the computations than to try to store/mark/compute the rotations and flips of the board.
Something like this:
for (kXX = kYY - 1; kXX > (11-XX); --kXX) {
threat_XX = threat_YY | pre_computed_threat[kXX];
for (kZZ = KXX - 1; kZZ > (11-ZZ); --kZZ) {
threat_ZZ = threat_XX | pre_computed_threat[kZZ];
The nice thing about this approach is that your objective is total coverage by threats - all 1 bits in the threat_11 map, in other words. You can test just by inverting the bits and comparing with zero.
Well thanks to my questions of today I got myself blocked from asking more until my rep gets better, but for what its worth I managed to solve the problem myself. I had to take 3 positions in each quarter of the board that can only be reached by seperate horses, that way making my 12 for cycles shorter, as they only had to go through specific locations, instead of trying every single of of them. The execution times is just above one second and it finds two different layouts for 12 knights, marking them 'H' and the positions they can reach in one turn 'T'. Here is the code if anyone ever comes to a problem like this:
#include <stdio.h>
#include <stdlib.h>
struct horses
{
int x;
int y;
};
void fill(struct horses arkliai[12], char array[8][8])
{
int i,j;
for(i = 0; i < 8; i++)
{
for(j = 0; j < 8; j++)
array[i][j] = '0';
}
for(i = 0; i < 12; i++)
{
array[arkliai[i].x][arkliai[i].y] = 'H';
}
pildymas(array);
}
void startingpositions(struct horses arkliai[12])
{
int i = 0;
int j = 0;
int a = 0;
for(a = 0; a < 12; a++)
{
if(i > 7)
{
i = 0;
j++;
}
arkliai[a].x = i;
arkliai[a].y = j;
i++;
}
}
void combinacijos(struct horses h[12], char array[8][8])
{
int a,b,c,d,e,f,g,hi,ii,ji,k,l;
for(a = 0; a < 2; a++)
{
if(a == 0)
{
h[0].x = 1;
h[0].y = 2;
}
if(a == 1)
{
h[0].x = 2;
h[0].y = 1;
}
for(b = 0; b < 2; b++)
{
if(b == 0)
{
h[1].x = 5;
h[1].y = 1;
}
if(b == 1)
{
h[1].x = 6;
h[1].y = 2;
}
for(c = 0; c < 2; c++)
{
if(c == 0)
{
h[2].x = 1;
h[2].y = 5;
}
if(c == 1)
{
h[2].x = 2;
h[2].y = 6;
}
for(d = 0; d <2;d++)
{
if(d == 0)
{
h[3].x = 5;
h[3].y = 6;
}
if(d == 1)
{
h[3].x = 6;
h[3].y = 5;
}
for(e = 0; e < 3; e++)
{
if(e == 0)
{
h[4].x = 2;
h[4].y = 0;
}
if(e == 1)
{
h[4].x = 2;
h[4].y = 2;
}
if(e == 2)
{
h[4].x = 1;
h[4].y = 3;
}
for (f = 0; f < 3; f++)
{
if(f == 0)
{
h[5].x = 1;
h[5].y = 4;
}
if(f == 1)
{
h[5].x = 2;
h[5].y = 5;
}
if(f == 2)
{
h[5].x = 2;
h[5].y = 7;
}
for (g = 0; g < 3; g++)
{
if(g == 0)
{
h[6].x = 5;
h[6].y = 7;
}
if(g == 1)
{
h[6].x = 5;
h[6].y = 5;
}
if(g == 2)
{
h[6].x = 6;
h[6].y = 4;
}
for(hi = 0; hi < 3; hi++)
{
if(hi == 0)
{
h[7].x = 5;
h[7].y = 0;
}
if(hi == 1)
{
h[7].x = 5;
h[7].y = 2;
}
if(hi == 2)
{
h[7].x = 6;
h[7].y = 3;
}
for(ii = 0; ii < 4; ii++)
{
if (ii == 0)
{
h[8].x = 3;
h[8].y = 0;
}
if (ii == 1)
{
h[8].x = 3;
h[8].y = 2;
}
if (ii == 2)
{
h[8].x = 0;
h[8].y = 3;
}
if (ii == 3)
{
h[8].x = 2;
h[8].y = 3;
}
for(ji = 0; ji < 4; ji++)
{
if (ji == 0)
{
h[9].x = 3;
h[9].y = 7;
}
if (ji == 1)
{
h[9].x = 3;
h[9].y = 5;
}
if (ji == 2)
{
h[9].x = 2;
h[9].y = 4;
}
if (ji == 3)
{
h[9].x = 0;
h[9].y = 4;
}
for(k = 0; k < 4; k++)
{
if (k == 0)
{
h[10].x = 4;
h[10].y = 7;
}
if (k == 1)
{
h[10].x = 4;
h[10].y = 5;
}
if (k == 2)
{
h[10].x = 5;
h[10].y = 4;
}
if (k == 3)
{
h[10].x = 7;
h[10].y = 4;
}
for(l = 0;l < 64;l++)
{
if(h[11].x == 7)
{
if(h[11].y == 7)
{
h[11].x = 0;
h[11].y = 0;
break;
}
h[11].x = 0;
h[11].y = h[11].y +1;
}
else { h[11].x= h[11].x+1; }
fill(h, array);
}
}
}
}
}
}
}
}
}
}
}
}
}
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';
}
}
}
tikrinimas(array);
}
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;
}
}
printas(array);
}
int printas(char array[8][8])
{
int i,j;
for(j = 0; j <8; j++)
{
printf("\n");
for(i = 0; i <8 ; i++)
printf("%c ", array[i][7-j]);
}
printf("\n");
}
int main()
{
struct horses hr[12];
char board[8][8];
startingpositions(hr);
combinacijos(hr, board);
return 0;
}

Uiimage to Base64 and back

I tri to convert NSData to Base64 to save an UIImage in the database, in a blob, that is located at a remote server.
To parse:
NSData *imagenData = UIImagePNGRepresentation(self.imagen.image);
MetodosComunes *metodos = [[MetodosComunes alloc] init];
NSString *imagenStr = [metodos base64forData:imagenData];
The method base64forData:
-(NSString*)base64forData:(NSData*)theData {
const uint8_t* input = (const uint8_t*)[theData bytes];
NSInteger length = [theData length];
static char table[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=";
NSMutableData* data = [NSMutableData dataWithLength:((length + 2) / 3) * 4];
uint8_t* output = (uint8_t*)data.mutableBytes;
NSInteger i;
for (i=0; i < length; i += 3) {
NSInteger value = 0;
NSInteger j;
for (j = i; j < (i + 3); j++) {
value <<= 8;
if (j < length) {
value |= (0xFF & input[j]);
}
}
NSInteger theIndex = (i / 3) * 4;
output[theIndex + 0] = table[(value >> 18) & 0x3F];
output[theIndex + 1] = table[(value >> 12) & 0x3F];
output[theIndex + 2] = (i + 1) < length ? table[(value >> 6) & 0x3F] : '=';
output[theIndex + 3] = (i + 2) < length ? table[(value >> 0) & 0x3F] : '=';
}
return [[[NSString alloc] initWithData:data encoding:NSASCIIStringEncoding] autorelease];
}
Then, i need to show this saved image at the application, and i convert base64 to NSData:
- (NSData *)base64DataFromString: (NSString *)string
{
unsigned long ixtext, lentext;
unsigned char ch, inbuf[4], outbuf[3];
short i, ixinbuf;
Boolean flignore, flendtext = false;
const unsigned char *tempcstring;
NSMutableData *theData;
if (string == nil)
{
return [NSData data];
}
ixtext = 0;
tempcstring = (const unsigned char *)[string UTF8String];
lentext = [string length];
theData = [NSMutableData dataWithCapacity: lentext];
ixinbuf = 0;
while (true)
{
if (ixtext >= lentext)
{
break;
}
ch = tempcstring [ixtext++];
flignore = false;
if ((ch >= 'A') && (ch <= 'Z'))
{
ch = ch - 'A';
}
else if ((ch >= 'a') && (ch <= 'z'))
{
ch = ch - 'a' + 26;
}
else if ((ch >= '0') && (ch <= '9'))
{
ch = ch - '0' + 52;
}
else if (ch == '+')
{
ch = 62;
}
else if (ch == '=')
{
flendtext = true;
}
else if (ch == '/')
{
ch = 63;
}
else
{
flignore = true;
}
if (!flignore)
{
short ctcharsinbuf = 3;
Boolean flbreak = false;
if (flendtext)
{
if (ixinbuf == 0)
{
break;
}
if ((ixinbuf == 1) || (ixinbuf == 2))
{
ctcharsinbuf = 1;
}
else
{
ctcharsinbuf = 2;
}
ixinbuf = 3;
flbreak = true;
}
inbuf [ixinbuf++] = ch;
if (ixinbuf == 4)
{
ixinbuf = 0;
outbuf[0] = (inbuf[0] << 2) | ((inbuf[1] & 0x30) >> 4);
outbuf[1] = ((inbuf[1] & 0x0F) << 4) | ((inbuf[2] & 0x3C) >> 2);
outbuf[2] = ((inbuf[2] & 0x03) << 6) | (inbuf[3] & 0x3F);
for (i = 0; i < ctcharsinbuf; i++)
{
[theData appendBytes: &outbuf[i] length: 1];
}
}
if (flbreak)
{
break;
}
}
}
return theData;
}
When i show the image, the image is black. At the console appear this error:
May 16 17:04:13 MacBook-Pro-de-Alejandro.local pantallas[1608] : ImageIO: PNG invalid PNG file: iDOT doesn't point to valid IDAT chunk May 16 17:04:13 MacBook-Pro-de-Alejandro.local pantallas[1608] : ImageIO: PNG Extra compressed data

Resources