-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathms.c
More file actions
executable file
·104 lines (90 loc) · 3.3 KB
/
ms.c
File metadata and controls
executable file
·104 lines (90 loc) · 3.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
#include <stdlib.h>
#include <stdio.h>
#include <time.h>
#define ORDER 3
int rrange(int, int);
int main()
{
srand(time(NULL));
int addTo = rrange(10, 200);
int square[ORDER][ORDER];
for(int i = 0; i < ORDER; i++)
{
for(int j = 0; j < ORDER; j++)
{
square[i][j] = 0;
}
}
int solved = 1;
printf("Welcome to the magic square game.the game of Nim.\n");
printf("You must fill the following grid of numbers, so that\n");
printf("each row, column and diagonal adds up to %i.\n", addTo);
printf("To enter a number in the grid, type the column, followed\n");
printf("by a comma, followed by the row, then another comma, then the\n");
printf("number to put into that square. For example: \n");
printf("2,3,14 would put the number 14 into the square in the second\n");
printf("column, third row.\n");
printf("====================================================\n");
do
{
printf("Column number: 1 2 3\n");
printf(" +---+---+---+\n");
printf("Row 1 |%3i|%3i|%3i|\n", square[0][0], square[1][0], square[2][0]);
printf(" +---+---+---+\n");
printf("Row 2 |%3i|%3i|%3i|\n", square[0][1], square[1][1], square[2][1]);
printf(" +---+---+---+\n");
printf("Row 3 |%3i|%3i|%3i|\n", square[0][2], square[1][2], square[2][2]);
printf(" +---+---+---+\n");
if(solved == 0)
{
printf("You solved the magic square!\n");
return 0;
}
printf("Please enter a column, row and number to put into the square (or type -1,-1,-1 to quit): ");
int row = 0, column = 0, num = 0;
int temp = scanf("%i,%i,%i", &column, &row, &num);
if(temp == EOF)
{
printf("Please enter a valid input, according to the instructions\n");
printf("defined at the beginning of this game.\n");
}
else if(row == -1 && column == -1 && num == -1)
{
return 0;
}
else if(column < 0 || column > ORDER)
{
printf("Please enter a column number between 0 and 3.\n");
}
else if(row < 0 || row > ORDER)
{
printf("Please enter a row number between 0 and 3.\n");
}
else if(num < 0 || num > addTo)
{
printf("Please enter a number at least 0, and not more than %i.\n", addTo);
}
else
{
square[column - 1][row - 1] = num;
if((square[0][0] + square[1][0] + square[2][0]) != addTo) solved = 0;
if((square[0][1] + square[1][1] + square[2][1]) != addTo) solved = 0;
if((square[0][2] + square[1][2] + square[2][2]) != addTo) solved = 0;
if((square[0][0] + square[0][1] + square[0][2]) != addTo) solved = 0;
if((square[1][0] + square[1][1] + square[1][2]) != addTo) solved = 0;
if((square[2][0] + square[2][1] + square[2][2]) != addTo) solved = 0;
if((square[0][0] + square[1][1] + square[2][2]) != addTo) solved = 0;
if((square[0][2] + square[1][1] + square[2][0]) != addTo) solved = 0;
}
}
while(1);
}
int rrange(int a, int b)
{
int temp = rand();
while(temp < a || temp > b)
{
temp = rand();
}
return temp;
}