I am trying to write a program for my 15 puzzle poject, but im continuing to get
ID: 3573297 • Letter: I
Question
I am trying to write a program for my 15 puzzle poject, but im continuing to get compiling errors becuase of the sleep, gotoxy and time functions in my program using visual studios 2015.I have tried to look up possible fixes to this online, but none of it helped. COULD SOMEONE PLEASE HELP ME FIX THESE PROBELMS this is what i have so far.
#include<stdio.h>
#include<conio.h>
#include<dos.h>
#include<Windows.h>
#ifdef _MSC_VER
#define _CRT_SECURE_NO_WARNINGS
#endif
int table[4][4];
int uniq[16] = { 1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1 };
void displayTable(void);
void moveNumbers(void);
void moveRight(void);
void moveLeft(void);
void moveUp(void);
void moveDown(void);
void ownsetup(void);
void randomizer(void);
int checkForFinish(void);
void showSqrNum(void);
// added int return type for main(). In C, it's always int main.
int main(void)
{
char choice;
int r;
for (r = 0; r < 3; r++)
printf(" ");
gotoxy(1, 2);
printf("Hello, and welcome to Hunter's 15-puzzle! ");
printf("To play a Randomly Generated Puzzle Press r ");
printf("To set up your own puzzle press o ");
choice = getchar();
if (choice == 'r')
randomizer();
if (choice == 'o')
ownsetup();
//pulls unwanted strokes off the keyboard buffer
printf(" press enter when ready");
while ((choice = getchar()) != ' ');
choice = getchar(); choice++; //holds the text window open
return 0;
}
//function used to display the table
void displayTable() {
int r, c;
gotoxy(1, 5);
for (r = 0; r < 4; r++) {
gotoxy(1, r + 5);
for (c = 0; c < 4; c++) {
printf("%4d", table[r][c]);
}
}
}
//function used to set up your own grid
void ownsetup() {
int x, r, c, sqr;
printf(" You will now be asked to place each number. ");
printf("When you are given a square number, please type in the ");
printf("number you want placed there. ");
for (r = 0; r < 3; r++)
printf(" ");
showSqrNum();
for (sqr = 0; sqr < 16; sqr++) {
do {
gotoxy(2, 12);
printf(" ");
printf(" ");
gotoxy(10, 12);
printf("Square %d: ", sqr + 1);
scanf_s("%d", &x);
if (!uniq[x]) {
x = -1;
}
if (x < 0 || x > 15) {
gotoxy(2, 12);
printf("Numbers must be a unique number, from 0 to 15. ");
printf("Please re-enter a number.");
sleep(3);
}
else {
r = sqr / 4;
c = sqr % 4;
table[r][c] = x;
uniq[x] = 0;
displayTable();
}
} while (x < 0 || x > 15);
}
displayTable();
}
//moves the number to the right
void moveRight() {
int temp, i, j;
for (i = 0; i < 4; i++) {
for (j = 0; j < 4; j++) {
if (table[i][j] == 0 && j != 0) {
temp = table[i][j - 1];
table[i][j - 1] = 0;
table[i][j] = temp;
}
}
}
}
//moves the number to the left
void moveLeft() {
int temp, i, j;
for (i = 3; i >= 0; i--) {
for (j = 3; j >= 0; j--) {
if (table[i][j] == 0 && j != 3) {
temp = table[i][j + 1];
table[i][j + 1] = 0;
table[i][j] = temp;
}
}
}
}
//moves the number up
void moveUp() {
int temp, i, j;
for (i = 3; i >= 0; i--) {
for (j = 3; j >= 0; j--) {
if (table[i][j] == 0 && i != 3) {
temp = table[i + 1][j];
table[i + 1][j] = 0;
table[i][j] = temp;
}
}
}
}
//moves the number down
void moveDown() {
int temp, i, j;
for (i = 0; i < 4; i++) {
for (j = 0; j < 4; j++) {
if (table[i][j] == 0 && i != 0) {
temp = table[i - 1][j];
table[i - 1][j] = 0;
table[i][j] = temp;
}
}
}
}
//function used to move the numbers in the grid
void moveNumbers() {
int key;
printf(" Enter a letter(I, J, K or M): ");
key = getchar();
if (key == 'j')
moveLeft();
if (key == 'k')
moveRight();
if (key == 'i')
moveUp();
if (key == 'm')
moveDown();
displayTable();
}
//function used to randomize the grid
void randomizer() {
//?? keep with int's. You have no need for char's in the puzzle table
// mixing char's with int's when you don't need to, just
// complicates things.
// char unsigned x = rand()%4;
int x;
int i = 50;
srand(time(NULL));
x = rand() % 4;
while (i>1) {
if (x == 0)
{
moveUp();
}
else if (x == 1)
{
moveDown();
}
else if (x == 2)
{
moveLeft();
}
else if (x == 3)
{
moveRight();
}
i--;
}
displayTable();
}
//function used to spot a winning board
int checkForFinish(void) {
int i, j, temp, result;
result = 0;
temp = 1;
for (i = 0; i < 4; i++) {
for (j = 0; j < 4; j++) {
if (table[i][j] == temp) {
temp += 1;
}
}
}
if (temp == 15)
result = 1;
return result;
// mising brace
}
//shows the square number of the board, for ownsetup() only
void showSqrNum(void) {
int r, c;
for (r = 0; r < 20; r++) {
gotoxy(30, r + 5);
printf(" ");
}
for (r = 0; r < 4; r++) {
gotoxy(30, r + 5);
for (c = 0; c < 4; c++) {
printf("%4d", (r * 4 + c + 1));
}
}
printf(" ");
r = getchar();
}
Explanation / Answer
//try this
using System;
using System.Drawing;
using System.Linq;
using System.Windows.Forms;
public class FifteenPuzzle
{
const int gridSize = 4; //Standard 15 puzzle is 4x4
const bool evenSized = gridSize % 2 == 0;
const int blockCount = gridSize * gridSize;
const int last = blockCount - 1;
const int buttonSize = 50;
const int buttonMargin = 3; //default = 3
const int formEdge = 9;
static readonly Random rnd = new Random();
static readonly Font buttonFont = new Font("Arial", 15.75F, FontStyle.Regular, GraphicsUnit.Point, ((byte)(0)));
readonly Button[] buttons = new Button[blockCount];
readonly int[] grid = new int[blockCount];
readonly int[] positionOf = new int[blockCount];
int moves = 0;
DateTime start;
public static void Main(string[] args)
{
FifteenPuzzle p = new FifteenPuzzle();
Form f = p.BuildForm();
Application.Run(f);
}
public FifteenPuzzle()
{
for (int i = 0; i < blockCount; i++) {
grid[i] = i;
positionOf[i] = i;
}
}
Form BuildForm()
{
Button startButton = new Button {
Font = new Font("Arial", 9.75F, FontStyle.Regular, GraphicsUnit.Point, ((byte)(0))),
Size = new Size(86, 23),
Location = new Point(formEdge,
(buttonSize + buttonMargin * 2) * gridSize + buttonMargin + formEdge),
Text = "New Game",
UseVisualStyleBackColor = true
};
startButton.Click += (sender, e) => Shuffle();
int size = buttonSize * gridSize + buttonMargin * gridSize * 2 + formEdge * 2;
Form form = new Form {
Text = "Fifteen",
ClientSize = new Size(width: size, height: size + buttonMargin * 2 + startButton.Height)
};
form.SuspendLayout();
for (int index = 0; index < blockCount; index++) {
Button button = new Button {
Font = buttonFont,
Size = new Size(buttonSize, buttonSize),
//Margin = new Padding(buttonMargin),
Text = (index + 1).ToString(),
UseVisualStyleBackColor = true
};
SetLocation(button, index);
form.Controls.Add(button);
buttons[index] = button;
int i = index;
button.Click += (sender, e) => ButtonClick(i);
}
form.Controls.Add(startButton);
form.ResumeLayout();
return form;
}
void ButtonClick(int i)
{
if (buttons[last].Visible) return;
int target = positionOf[i];
if (positionOf[i] / gridSize == positionOf[last] / gridSize) {
while (positionOf[last] < target) {
Swap(last, grid[positionOf[last] + 1]);
moves++;
}
while (positionOf[last] > target) {
Swap(last, grid[positionOf[last] - 1]);
moves++;
}
} else if (positionOf[i] % gridSize == positionOf[last] % gridSize) {
while (positionOf[last] < target) {
Swap(last, grid[positionOf[last] + gridSize]);
moves++;
}
while (positionOf[last] > target) {
Swap(last, grid[positionOf[last] - gridSize]);
moves++;
}
}
if (Solved()) {
TimeSpan elapsed = DateTime.Now - start;
elapsed = TimeSpan.FromSeconds(Math.Round(elapsed.TotalSeconds, 0));
buttons[last].Visible = true;
MessageBox.Show($"Solved in {moves} moves. Time: {elapsed}");
}
}
bool Solved() => Enumerable.Range(0, blockCount - 1).All(i => positionOf[i] == i);
static void SetLocation(Button button, int index)
{
int row = index / gridSize, column = index % gridSize;
button.Location = new Point(
(buttonSize + buttonMargin * 2) * column + buttonMargin + formEdge,
(buttonSize + buttonMargin * 2) * row + buttonMargin + formEdge);
}
void Shuffle()
{
for (int i = 0; i < blockCount; i++) {
int r = rnd.Next(i, blockCount);
int g = grid[r];
grid[r] = grid[i];
grid[i] = g;
}
for (int i = 0; i < blockCount; i++) {
positionOf[grid[i]] = i;
SetLocation(buttons[grid[i]], i);
}
if (!Solvable()) Swap(0, 1); //Swap any 2 blocks
buttons[last].Visible = false;
moves = 0;
start = DateTime.Now;
}
bool Solvable()
{
bool parity = true;
for (int i = 0; i < blockCount - 2; i++) {
for (int j = i + 1; j < blockCount - 1; j++) {
if (positionOf[j] < positionOf[i]) parity = !parity;
}
}
if (evenSized && positionOf[last] / gridSize % 2 == 0) parity = !parity;
return parity;
}
void Swap(int a, int b)
{
Point location = buttons[a].Location;
buttons[a].Location = buttons[b].Location;
buttons[b].Location = location;
int p = positionOf[a];
positionOf[a] = positionOf[b];
positionOf[b] = p;
grid[positionOf[a]] = a;
grid[positionOf[b]] = b;
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.