added more code
This commit is contained in:
1
CPE455/.vscode/configurationCache.log
vendored
Normal file
1
CPE455/.vscode/configurationCache.log
vendored
Normal file
@ -0,0 +1 @@
|
||||
{"buildTargets":["make"],"launchTargets":[],"customConfigurationProvider":{"workspaceBrowse":{"browsePath":[],"compilerArgs":[]},"fileIndex":[]}}
|
6
CPE455/.vscode/dryrun.log
vendored
Normal file
6
CPE455/.vscode/dryrun.log
vendored
Normal file
@ -0,0 +1,6 @@
|
||||
make --dry-run --always-make --keep-going --print-directory
|
||||
make: Entering directory `/home/student/anw0044/CPE455'
|
||||
make: Leaving directory `/home/student/anw0044/CPE455'
|
||||
|
||||
make: *** No targets specified and no makefile found. Stop.
|
||||
|
3
CPE455/.vscode/settings.json
vendored
Normal file
3
CPE455/.vscode/settings.json
vendored
Normal file
@ -0,0 +1,3 @@
|
||||
{
|
||||
"makefile.extensionOutputFolder": "./.vscode"
|
||||
}
|
307
CPE455/.vscode/targets.log
vendored
Normal file
307
CPE455/.vscode/targets.log
vendored
Normal file
@ -0,0 +1,307 @@
|
||||
make all --print-data-base --no-builtin-variables --no-builtin-rules --question
|
||||
# GNU Make 3.82
|
||||
# Built for x86_64-redhat-linux-gnu
|
||||
# Copyright (C) 2010 Free Software Foundation, Inc.
|
||||
# License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>
|
||||
# This is free software: you are free to change and redistribute it.
|
||||
# There is NO WARRANTY, to the extent permitted by law.
|
||||
|
||||
# Make data base, printed on Sun Apr 17 17:47:25 2022
|
||||
|
||||
# Variables
|
||||
|
||||
# automatic
|
||||
<D = $(patsubst %/,%,$(dir $<))
|
||||
# automatic
|
||||
?F = $(notdir $?)
|
||||
# environment
|
||||
DESKTOP_SESSION = gnome-classic
|
||||
# default
|
||||
.SHELLFLAGS := -c
|
||||
# environment
|
||||
XDG_SESSION_TYPE = x11
|
||||
# environment
|
||||
VSCODE_LOG_NATIVE = false
|
||||
# environment
|
||||
MODULEPATH = /usr/share/Modules/modulefiles:/etc/modulefiles
|
||||
# automatic
|
||||
?D = $(patsubst %/,%,$(dir $?))
|
||||
# automatic
|
||||
@D = $(patsubst %/,%,$(dir $@))
|
||||
# environment
|
||||
XAUTHORITY = /run/gdm/auth-for-anw0044-B34xeN/database
|
||||
# environment
|
||||
GDMSESSION = gnome-classic
|
||||
# environment
|
||||
XMODIFIERS = @im=ibus
|
||||
# makefile
|
||||
CURDIR := /home/student/anw0044/CPE455
|
||||
# makefile
|
||||
SHELL = /bin/sh
|
||||
# environment
|
||||
GDM_LANG = en_US.UTF-8
|
||||
# environment
|
||||
VSCODE_NLS_CONFIG = {"locale":"en-us","availableLanguages":{},"_languagePackSupport":true}
|
||||
# environment
|
||||
_ = /usr/bin/make
|
||||
# environment
|
||||
ftp_proxy = http://172.21.0.47:3128/
|
||||
# environment
|
||||
LESSOPEN = ||/usr/bin/lesspipe.sh %s
|
||||
# makefile
|
||||
MAKEFILE_LIST :=
|
||||
# automatic
|
||||
@F = $(notdir $@)
|
||||
# environment
|
||||
GIO_LAUNCHED_DESKTOP_FILE = /usr/share/applications/code.desktop
|
||||
# environment
|
||||
USERNAME = anw0044
|
||||
# environment
|
||||
XDG_DATA_DIRS = /home/student/anw0044/.local/share/flatpak/exports/share/:/var/lib/flatpak/exports/share/:/usr/local/share/:/usr/share/
|
||||
# environment
|
||||
DBUS_SESSION_BUS_ADDRESS = unix:abstract=/tmp/dbus-wu12yYMlPQ,guid=77c72388ccb7c37e648a0b44625c7186
|
||||
# environment
|
||||
VSCODE_VERBOSE_LOGGING = true
|
||||
# environment
|
||||
VSCODE_IPC_HOOK_EXTHOST = /run/user/17833/vscode-ipc-87dc8f0e-20dd-4ecd-9ddd-03a62c70c1c5.sock
|
||||
# environment
|
||||
VSCODE_CWD = /home/student/anw0044
|
||||
# environment
|
||||
HISTCONTROL = ignoredups
|
||||
# environment
|
||||
PATH = /opt/Qt/5.12.4/gcc_64/bin/:/opt/Qt/Tools/QtCreator/bin:/opt/Qt/5.12.4/gcc_64/bin/:/opt/Qt/Tools/QtCreator/bin:/usr/local/bin:/usr/local/sbin:/usr/bin:/usr/sbin:/bin:/sbin
|
||||
# environment
|
||||
LM_LICENSE_FILE = 1717@lm1
|
||||
# environment
|
||||
SESSION_MANAGER = local/unix:@/tmp/.ICE-unix/23266,unix/unix:/tmp/.ICE-unix/23266
|
||||
# environment
|
||||
XDG_RUNTIME_DIR = /run/user/17833
|
||||
# environment
|
||||
XDG_MENU_PREFIX = gnome-
|
||||
# environment
|
||||
VSCODE_LOG_STACK = false
|
||||
# environment
|
||||
ELECTRON_RUN_AS_NODE = 1
|
||||
# default
|
||||
.FEATURES := target-specific order-only second-expansion else-if shortest-stem undefine oneshell archives jobserver check-symlink
|
||||
# environment
|
||||
XDG_SESSION_DESKTOP = gnome-classic
|
||||
# environment
|
||||
SSH_AUTH_SOCK = /run/user/17833/keyring/ssh
|
||||
# environment
|
||||
GIO_LAUNCHED_DESKTOP_FILE_PID = 24204
|
||||
# automatic
|
||||
%F = $(notdir $%)
|
||||
# environment
|
||||
DISPLAY = :0
|
||||
# environment
|
||||
IMSETTINGS_INTEGRATE_DESKTOP = yes
|
||||
# environment
|
||||
PWD = /home/student/anw0044/CPE455
|
||||
# environment
|
||||
XDG_SEAT = seat0
|
||||
# environment
|
||||
SSH_AGENT_PID = 23391
|
||||
# environment
|
||||
ORIGINAL_XDG_CURRENT_DESKTOP = GNOME-Classic:GNOME
|
||||
# environment
|
||||
VSCODE_AMD_ENTRYPOINT = vs/workbench/api/node/extensionHostProcess
|
||||
# environment
|
||||
HOME = /home/student/anw0044
|
||||
# environment
|
||||
LD_LIBRARY_PATH = /opt/Qt/5.12.4/gcc_64/lib:/opt/Qt/5.12.4/gcc_64/lib:
|
||||
# environment
|
||||
VSCODE_CODE_CACHE_PATH = /home/student/anw0044/.config/Code/CachedData/c722ca6c7eed3d7987c0d5c3df5c45f6b15e77d1
|
||||
# environment
|
||||
LOGNAME = anw0044
|
||||
# environment
|
||||
APPLICATION_INSIGHTS_NO_DIAGNOSTIC_CHANNEL = true
|
||||
# environment
|
||||
VSCODE_HANDLES_UNCAUGHT_ERRORS = true
|
||||
# default
|
||||
MAKE_VERSION := 3.82
|
||||
# environment
|
||||
GNOME_DESKTOP_SESSION_ID = this-is-deprecated
|
||||
# automatic
|
||||
^D = $(patsubst %/,%,$(dir $^))
|
||||
# environment
|
||||
HOSTNAME = eb246-19
|
||||
# environment
|
||||
XDG_VTNR = 1
|
||||
# environment
|
||||
MAKELEVEL := 0
|
||||
# default
|
||||
MAKE = $(MAKE_COMMAND)
|
||||
# default
|
||||
MAKECMDGOALS := all
|
||||
# environment
|
||||
QT_IM_MODULE = ibus
|
||||
# environment
|
||||
SHLVL = 3
|
||||
# environment
|
||||
GNOME_SHELL_SESSION_MODE = classic
|
||||
# environment
|
||||
XDG_SESSION_ID = 4452
|
||||
# environment
|
||||
USER = anw0044
|
||||
# makefile
|
||||
.DEFAULT_GOAL :=
|
||||
# environment
|
||||
XDG_CURRENT_DESKTOP = GNOME-Classic:GNOME
|
||||
# automatic
|
||||
%D = $(patsubst %/,%,$(dir $%))
|
||||
# environment
|
||||
define BASH_FUNC_module()
|
||||
() { eval `/usr/bin/modulecmd bash $*`
|
||||
}
|
||||
endef
|
||||
# default
|
||||
MAKE_COMMAND := make
|
||||
# default
|
||||
.VARIABLES :=
|
||||
# environment
|
||||
IMSETTINGS_MODULE = none
|
||||
# automatic
|
||||
*F = $(notdir $*)
|
||||
# environment
|
||||
VSCODE_IPC_HOOK = /run/user/17833/vscode-cca9002f-1.65.2-main.sock
|
||||
# makefile
|
||||
MAKEFLAGS = Rrqp
|
||||
# environment
|
||||
MFLAGS = -Rrqp
|
||||
# automatic
|
||||
*D = $(patsubst %/,%,$(dir $*))
|
||||
# environment
|
||||
MAIL = /var/spool/mail/anw0044
|
||||
# environment
|
||||
LOADEDMODULES =
|
||||
# automatic
|
||||
+D = $(patsubst %/,%,$(dir $+))
|
||||
# environment
|
||||
GDK_BACKEND = x11
|
||||
# environment
|
||||
MODULESHOME = /usr/share/Modules
|
||||
# automatic
|
||||
+F = $(notdir $+)
|
||||
# environment
|
||||
NO_AT_BRIDGE = 1
|
||||
# environment
|
||||
VSCODE_PIPE_LOGGING = true
|
||||
# default
|
||||
MAKEFILES :=
|
||||
# automatic
|
||||
<F = $(notdir $<)
|
||||
# environment
|
||||
https_proxy = https://172.21.0.47:3128/
|
||||
# environment
|
||||
LC_ALL = C
|
||||
# environment
|
||||
SBX_CHROME_API_RQ = 1
|
||||
# environment
|
||||
SANDBOX_LD_LIBRARY_PATH = /opt/Qt/5.12.4/gcc_64/lib:
|
||||
# environment
|
||||
GJS_DEBUG_TOPICS = JS ERROR;JS LOG
|
||||
# automatic
|
||||
^F = $(notdir $^)
|
||||
# default
|
||||
SUFFIXES :=
|
||||
# environment
|
||||
HISTSIZE = 1000
|
||||
# environment
|
||||
CHROME_DESKTOP = code-url-handler.desktop
|
||||
# environment
|
||||
WINDOWPATH = 1:1:1:1:1:1:1:1:1:1:1:1
|
||||
# default
|
||||
.INCLUDE_DIRS = /usr/include /usr/local/include /usr/include
|
||||
# environment
|
||||
GJS_DEBUG_OUTPUT = stderr
|
||||
# default
|
||||
.RECIPEPREFIX :=
|
||||
# environment
|
||||
http_proxy = http://172.21.0.47:3128/
|
||||
# environment
|
||||
LANG = C
|
||||
# environment
|
||||
TERM = dumb
|
||||
# environment
|
||||
VSCODE_PID = 24204
|
||||
# variable set hash-table stats:
|
||||
# Load=106/1024=10%, Rehash=0, Collisions=11/125=9%
|
||||
|
||||
# Pattern-specific Variable Values
|
||||
|
||||
# No pattern-specific variable values.
|
||||
|
||||
# Directories
|
||||
|
||||
# . (device 40, inode 191762795): 17 files, no impossibilities.
|
||||
|
||||
# 17 files, no impossibilities in 1 directories.
|
||||
|
||||
# Implicit Rules
|
||||
|
||||
# No implicit rules.
|
||||
|
||||
# Files
|
||||
|
||||
# Not a target:
|
||||
all:
|
||||
# Command line target.
|
||||
# Implicit rule search has been done.
|
||||
# File does not exist.
|
||||
# File has not been updated.
|
||||
|
||||
# Not a target:
|
||||
.SUFFIXES:
|
||||
# Implicit rule search has not been done.
|
||||
# Modification time never checked.
|
||||
# File has not been updated.
|
||||
|
||||
# Not a target:
|
||||
Makefile:
|
||||
# Implicit rule search has been done.
|
||||
# File does not exist.
|
||||
# File has been updated.
|
||||
# Failed to be updated.
|
||||
|
||||
# Not a target:
|
||||
makefile:
|
||||
# Implicit rule search has been done.
|
||||
# File does not exist.
|
||||
# File has been updated.
|
||||
# Failed to be updated.
|
||||
|
||||
# Not a target:
|
||||
.DEFAULT:
|
||||
# Implicit rule search has not been done.
|
||||
# Modification time never checked.
|
||||
# File has not been updated.
|
||||
|
||||
# Not a target:
|
||||
GNUmakefile:
|
||||
# Implicit rule search has been done.
|
||||
# File does not exist.
|
||||
# File has been updated.
|
||||
# Failed to be updated.
|
||||
|
||||
# files hash-table stats:
|
||||
# Load=6/1024=1%, Rehash=0, Collisions=0/15=0%
|
||||
# VPATH Search Paths
|
||||
|
||||
make: *** No rule to make target `all'. Stop.
|
||||
|
||||
|
||||
# No `vpath' search paths.
|
||||
|
||||
# No general (`VPATH' variable) search path.
|
||||
|
||||
# # of strings in strcache: 0 / lookups = 28 / hits = 28
|
||||
# # of strcache buffers: 1 (* 8176 B/buffer = 8176 B)
|
||||
# strcache used: total = 0 (205) / max = 0 / min = 8176 / avg = 0
|
||||
# strcache free: total = 0 (7971) / max = 0 / min = 8176 / avg = 0
|
||||
|
||||
# strcache hash-table stats:
|
||||
# Load=25/8192=0%, Rehash=0, Collisions=1/28=4%
|
||||
# Finished Make data base on Sun Apr 17 17:47:25 2022
|
||||
|
||||
|
15
CPE455/Quiz-Bravo/README.txt
Normal file
15
CPE455/Quiz-Bravo/README.txt
Normal file
@ -0,0 +1,15 @@
|
||||
|
||||
bapp.c
|
||||
|
||||
sample code uses gets( )
|
||||
|
||||
|
||||
Correct password is the following fifteen characters: aaaaabbbbbccccc
|
||||
|
||||
|
||||
perl -e 'print "a"x90' | ./vulnerable_bapp
|
||||
|
||||
perl -e 'print "a"x90' | ./protected_bapp
|
||||
|
||||
|
||||
|
37
CPE455/Quiz-Bravo/bapp.c
Normal file
37
CPE455/Quiz-Bravo/bapp.c
Normal file
@ -0,0 +1,37 @@
|
||||
/*
|
||||
* bapp.c
|
||||
*
|
||||
*/
|
||||
|
||||
#include <stdio.h>
|
||||
#include <string.h>
|
||||
#include <signal.h>
|
||||
|
||||
#define BUFFER_SIZE 20
|
||||
|
||||
int main()
|
||||
{
|
||||
char correctPassword[BUFFER_SIZE];
|
||||
char suppliedPassword[BUFFER_SIZE];
|
||||
|
||||
printf("\nEnter the password now: ");
|
||||
strncpy(correctPassword, "aaaaabbbbbccccc", BUFFER_SIZE);
|
||||
gets(suppliedPassword);
|
||||
printf("\n");
|
||||
|
||||
if (strncmp(suppliedPassword, correctPassword, BUFFER_SIZE) == 0)
|
||||
{
|
||||
printf("Login successful\n\n");
|
||||
}
|
||||
else
|
||||
{
|
||||
printf("Login failure\n\n");
|
||||
}
|
||||
|
||||
printf("suppliedPassword: %s\n", suppliedPassword);
|
||||
printf("correctPassword: %s\n", correctPassword);
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
18
CPE455/Quiz-Bravo/makefile
Normal file
18
CPE455/Quiz-Bravo/makefile
Normal file
@ -0,0 +1,18 @@
|
||||
#
|
||||
# bapp.c makefile
|
||||
#
|
||||
|
||||
CC = gcc
|
||||
CFLAGS = -g
|
||||
|
||||
vulnerable_bapp: bapp.c
|
||||
$(CC) $(CFLAGS) -fno-stack-protector -z execstack bapp.c -o vulnerable_bapp
|
||||
|
||||
protected_bapp: bapp.c
|
||||
$(CC) $(CFLAGS) -fstack-protector -z noexecstack bapp.c -o protected_bapp
|
||||
|
||||
clean:
|
||||
rm vulnerable_bapp protected_bapp
|
||||
|
||||
|
||||
|
92
CPE455/Quiz-Bravo/protected.txt
Normal file
92
CPE455/Quiz-Bravo/protected.txt
Normal file
@ -0,0 +1,92 @@
|
||||
Script started on Thu 24 Feb 2022 10:16:22 AM CST
|
||||
[?1034hbash-4.2$ ./protected_bapp
|
||||
|
||||
Enter the password now: aaaa
|
||||
|
||||
Login failure
|
||||
|
||||
suppliedPassword: aaaa
|
||||
correctPassword: aaaaabbbbbccccc
|
||||
bash-4.2$ ./protected_bapp
|
||||
|
||||
Enter the password now: aaaaabbbbbccccc
|
||||
|
||||
Login successful
|
||||
|
||||
suppliedPassword: aaaaabbbbbccccc
|
||||
correctPassword: aaaaabbbbbccccc
|
||||
bash-4.2$ perl -e 'print "a"x90' | ./protected_bapp
|
||||
|
||||
Enter the password now:
|
||||
Login failure
|
||||
|
||||
suppliedPassword: aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
|
||||
correctPassword: aaaaabbbbbccccc
|
||||
*** stack smashing detected ***: ./protected_bapp terminated
|
||||
Segmentation fault
|
||||
bash-4.2$ gdb ./protected_bapp
|
||||
[?1034hGNU gdb (GDB) Red Hat Enterprise Linux 7.6.1-120.el7
|
||||
Copyright (C) 2013 Free Software Foundation, Inc.
|
||||
License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>
|
||||
This is free software: you are free to change and redistribute it.
|
||||
There is NO WARRANTY, to the extent permitted by law. Type "show copying"
|
||||
and "show warranty" for details.
|
||||
This GDB was configured as "x86_64-redhat-linux-gnu".
|
||||
For bug reporting instructions, please see:
|
||||
<http://www.gnu.org/software/gdb/bugs/>...
|
||||
Reading symbols from /home/student/anw0044/CPE455/Quiz-Bravo/protected_bapp...done.
|
||||
(gdb) break main
|
||||
Breakpoint 1 at 0x400716: file bapp.c, line 13.
|
||||
(gdb) run
|
||||
Starting program: /home/student/anw0044/CPE455/Quiz-Bravo/./protected_bapp
|
||||
|
||||
Breakpoint 1, main () at bapp.c:13
|
||||
13 {
|
||||
Missing separate debuginfos, use: debuginfo-install glibc-2.17-325.el7_9.x86_64
|
||||
(gdb) info locals
|
||||
correctPassword = "\000\000\000\000\000\000\000\000\377\265\360\000\000\000\000\000\001\000\000"
|
||||
suppliedPassword = "\320\340\377\377\377\177\000\000\000\000\000\000\000\000\000\000\340\a@"
|
||||
(gdb) r[Kstep
|
||||
17 printf("\nEnter the password now: ");
|
||||
(gdb) in[K[Kstepinfo locals
|
||||
correctPassword = "\000\000\000\000\000\000\000\000\377\265\360\000\000\000\000\000\001\000\000"
|
||||
suppliedPassword = "\320\340\377\377\377\177\000\000\000\000\000\000\000\000\000\000\340\a@"
|
||||
(gdb) info locals
|
||||
[C[C[C[C[C[C[7Pstep
|
||||
|
||||
18 strncpy(correctPassword, "aaaaabbbbbccccc", BUFFER_SIZE);
|
||||
(gdb) stepinfo locals
|
||||
correctPassword = "\000\000\000\000\000\000\000\000\377\265\360\000\000\000\000\000\001\000\000"
|
||||
suppliedPassword = "\320\340\377\377\377\177\000\000\000\000\000\000\000\000\000\000\340\a@"
|
||||
(gdb) info locals
|
||||
[C[C[C[C[C[C[7Pstep
|
||||
19 gets(suppliedPassword);
|
||||
(gdb) stepinfo locals
|
||||
correctPassword = "aaaaabbbbbccccc\000\000\000\000"
|
||||
suppliedPassword = "\320\340\377\377\377\177\000\000\000\000\000\000\000\000\000\000\340\a@"
|
||||
(gdb) info locals
|
||||
[C[C[C[C[C[C[7Pstep
|
||||
Enter the password now: aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
|
||||
20 printf("\n");
|
||||
(gdb) stepinfo locals
|
||||
correctPassword = "aaaaabbbbbccccc\000\000\000\000"
|
||||
suppliedPassword = 'a' <repeats 20 times>
|
||||
(gdb) info locals
|
||||
[C[C[C[C[C[C[7Pstepinfo locals
|
||||
[C[C[C[C[C[C[7Pstepinfo locals
|
||||
[C[C[C[C[C[C[Kcontinue
|
||||
Continuing.
|
||||
|
||||
Login failure
|
||||
|
||||
suppliedPassword: aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
|
||||
correctPassword: aaaaabbbbbccccc
|
||||
*** stack smashing detected ***: /home/student/anw0044/CPE455/Quiz-Bravo/./protected_bapp terminated
|
||||
|
||||
Program received signal SIGSEGV, Segmentation fault.
|
||||
0x00007ffff78060f8 in ?? () from /lib64/libgcc_s.so.1
|
||||
Missing separate debuginfos, use: debuginfo-install libgcc-4.8.5-39.el7.x86_64 libgcc-4.8.5-44.el7.x86_64
|
||||
(gdb) quit
|
||||
A debugging session is active.
|
||||
|
||||
Inferior 1 [process 6749] will be killed.
|
BIN
CPE455/Quiz-Bravo/protected_bapp
Executable file
BIN
CPE455/Quiz-Bravo/protected_bapp
Executable file
Binary file not shown.
94
CPE455/Quiz-Bravo/vulnerable.txt
Normal file
94
CPE455/Quiz-Bravo/vulnerable.txt
Normal file
@ -0,0 +1,94 @@
|
||||
Script started on Thu 24 Feb 2022 10:08:08 AM CST
|
||||
[?1034hbash-4.2$ ./vulnerable_bapp `[K
|
||||
|
||||
Enter the password now: aaaa
|
||||
|
||||
Login failure
|
||||
|
||||
suppliedPassword: aaaa
|
||||
correctPassword: aaaaabbbbbccccc
|
||||
bash-4.2$ ./vulnerable_bapp
|
||||
|
||||
Enter the password now: aaaaabbbbbccccc
|
||||
|
||||
Login successful
|
||||
|
||||
suppliedPassword: aaaaabbbbbccccc
|
||||
correctPassword: aaaaabbbbbccccc
|
||||
bash-4.2$ perl -e 'print "a"x90' | ./vulnerable_bapp
|
||||
|
||||
Enter the password now:
|
||||
Login successful
|
||||
|
||||
suppliedPassword: aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
|
||||
correctPassword: aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
|
||||
Segmentation fault
|
||||
bash-4.2$ perl -e 'print "a"x90' | ./protected_bapp
|
||||
|
||||
Enter the password now:
|
||||
Login failure
|
||||
|
||||
suppliedPassword: aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
|
||||
correctPassword: aaaaabbbbbccccc
|
||||
*** stack smashing detected ***: ./protected_bapp terminated
|
||||
Segmentation fault
|
||||
bash-4.2$ gdb ./vulnerable_bapp
|
||||
[?1034hGNU gdb (GDB) Red Hat Enterprise Linux 7.6.1-120.el7
|
||||
Copyright (C) 2013 Free Software Foundation, Inc.
|
||||
License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>
|
||||
This is free software: you are free to change and redistribute it.
|
||||
There is NO WARRANTY, to the extent permitted by law. Type "show copying"
|
||||
and "show warranty" for details.
|
||||
This GDB was configured as "x86_64-redhat-linux-gnu".
|
||||
For bug reporting instructions, please see:
|
||||
<http://www.gnu.org/software/gdb/bugs/>...
|
||||
Reading symbols from /home/student/anw0044/CPE455/Quiz-Bravo/vulnerable_bapp...done.
|
||||
(gdb)
|
||||
[K(gdb)
|
||||
[K(gdb) break main
|
||||
Breakpoint 1 at 0x4006a5: file bapp.c, line 17.
|
||||
(gdb) run
|
||||
Starting program: /home/student/anw0044/CPE455/Quiz-Bravo/./vulnerable_bapp
|
||||
|
||||
Breakpoint 1, main () at bapp.c:17
|
||||
17 printf("\nEnter the password now: ");
|
||||
Missing separate debuginfos, use: debuginfo-install glibc-2.17-325.el7_9.x86_64
|
||||
(gdb) info locals
|
||||
correctPassword = "P\a@\000\000\000\000\000\260\005@\000\000\000\000\000\260\341\377\377"
|
||||
suppliedPassword = "\001\000\000\000\000\000\000\000\235\a@\000\000\000\000\000\320\340\377\377"
|
||||
(gdb) step
|
||||
|
||||
18 strncpy(correctPassword, "aaaaabbbbbccccc", BUFFER_SIZE);
|
||||
(gdb) stepinfo locals
|
||||
correctPassword = "P\a@\000\000\000\000\000\260\005@\000\000\000\000\000\260\341\377\377"
|
||||
suppliedPassword = "\001\000\000\000\000\000\000\000\235\a@\000\000\000\000\000\320\340\377\377"
|
||||
(gdb) info locals
|
||||
[C[C[C[C[C[C[7Pstep
|
||||
19 gets(suppliedPassword);
|
||||
(gdb) stepinfo locals
|
||||
correctPassword = "aaaaabbbbbccccc\000\000\000\000"
|
||||
suppliedPassword = "\001\000\000\000\000\000\000\000\235\a@\000\000\000\000\000\320\340\377\377"
|
||||
(gdb) info locals
|
||||
[C[C[C[C[C[C[7Pstepinfo locals
|
||||
[C[C[C[C[C[C[7Pstepinfo locals
|
||||
[C[C[C[C[C[Crun[Kbreak main
|
||||
[C[C[C[C[C[Crun[Kinfo locals
|
||||
[C[C[C[C[C[C[7Pstepinfo locals
|
||||
[C[C[C[C[C[C[7Pstepinfo locals
|
||||
[C[C[C[C[C[C[Kstep
|
||||
Enter the password now: aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
|
||||
20 printf("\n");
|
||||
(gdb) stepinfo locals
|
||||
correctPassword = 'a' <repeats 20 times>
|
||||
suppliedPassword = 'a' <repeats 20 times>
|
||||
(gdb) continue
|
||||
Continuing.
|
||||
|
||||
Login successful
|
||||
|
||||
suppliedPassword: aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
|
||||
correctPassword: aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
|
||||
|
||||
Program received signal SIGSEGV, Segmentation fault.
|
||||
0x0000000000400744 in main () at bapp.c:35
|
||||
35 }
|
BIN
CPE455/Quiz-Bravo/vulnerable_bapp
Executable file
BIN
CPE455/Quiz-Bravo/vulnerable_bapp
Executable file
Binary file not shown.
15
CPE455/Quiz-Charlie/README.txt
Normal file
15
CPE455/Quiz-Charlie/README.txt
Normal file
@ -0,0 +1,15 @@
|
||||
|
||||
capp.c
|
||||
|
||||
sample code uses scanf( )
|
||||
|
||||
|
||||
Correct password is the following fifteen characters: aaaaabbbbbccccc
|
||||
|
||||
|
||||
perl -e 'print "a"x90' | ./vulnerable_capp
|
||||
|
||||
perl -e 'print "a"x90' | ./protected_capp
|
||||
|
||||
|
||||
|
94
CPE455/Quiz-Charlie/after.txt
Normal file
94
CPE455/Quiz-Charlie/after.txt
Normal file
@ -0,0 +1,94 @@
|
||||
Script started on Thu 24 Feb 2022 10:37:55 AM CST
|
||||
[?1034hbash-4.2$ ./vulnerable_capp
|
||||
|
||||
Enter the password now: aaaa
|
||||
|
||||
Login failure
|
||||
|
||||
suppliedPassword: aaaa
|
||||
sizeof(suppliedPassword) = 20
|
||||
strlen(suppliedPassword) = 4
|
||||
correctPassword: aaaaabbbbbccccc
|
||||
sizeof(correctPassword) = 20
|
||||
strlen(correctPassword) = 15
|
||||
|
||||
bash-4.2$ ./vulnerable_capp
|
||||
|
||||
Enter the password now: aaaaabbbbbccccc
|
||||
|
||||
Login successful
|
||||
|
||||
suppliedPassword: aaaaabbbbbccccc
|
||||
sizeof(suppliedPassword) = 20
|
||||
strlen(suppliedPassword) = 15
|
||||
correctPassword: aaaaabbbbbccccc
|
||||
sizeof(correctPassword) = 20
|
||||
strlen(correctPassword) = 15
|
||||
|
||||
bash-4.2$ perl -e 'print "a"x90' | ./vulnerable_capp
|
||||
|
||||
Enter the password now:
|
||||
Login failure
|
||||
|
||||
suppliedPassword: aaaaaaaaaaaaaaaaaaa
|
||||
sizeof(suppliedPassword) = 20
|
||||
strlen(suppliedPassword) = 19
|
||||
correctPassword: aaaaabbbbbccccc
|
||||
sizeof(correctPassword) = 20
|
||||
strlen(correctPassword) = 15
|
||||
|
||||
bash-4.2$ ./protected_capp
|
||||
|
||||
Enter the password now: aaaa
|
||||
|
||||
Login failure
|
||||
|
||||
suppliedPassword: aaaa
|
||||
sizeof(suppliedPassword) = 20
|
||||
strlen(suppliedPassword) = 4
|
||||
correctPassword: aaaaabbbbbccccc
|
||||
sizeof(correctPassword) = 20
|
||||
strlen(correctPassword) = 15
|
||||
|
||||
bash-4.2$ ./protected_capp
|
||||
|
||||
Enter the password now: aaaabbbbcccc
|
||||
|
||||
Login failure
|
||||
|
||||
suppliedPassword: aaaabbbbcccc
|
||||
sizeof(suppliedPassword) = 20
|
||||
strlen(suppliedPassword) = 12
|
||||
correctPassword: aaaaabbbbbccccc
|
||||
sizeof(correctPassword) = 20
|
||||
strlen(correctPassword) = 15
|
||||
|
||||
bash-4.2$ ./protected_capp
|
||||
|
||||
Enter the password now: aaaaabbbbbccccc
|
||||
|
||||
Login successful
|
||||
|
||||
suppliedPassword: aaaaabbbbbccccc
|
||||
sizeof(suppliedPassword) = 20
|
||||
strlen(suppliedPassword) = 15
|
||||
correctPassword: aaaaabbbbbccccc
|
||||
sizeof(correctPassword) = 20
|
||||
strlen(correctPassword) = 15
|
||||
|
||||
bash-4.2$ perl -e 'print "a"x90' | ./protected_capp
|
||||
|
||||
Enter the password now:
|
||||
Login failure
|
||||
|
||||
suppliedPassword: aaaaaaaaaaaaaaaaaaa
|
||||
sizeof(suppliedPassword) = 20
|
||||
strlen(suppliedPassword) = 19
|
||||
correctPassword: aaaaabbbbbccccc
|
||||
sizeof(correctPassword) = 20
|
||||
strlen(correctPassword) = 15
|
||||
|
||||
bash-4.2$ exit
|
||||
exit
|
||||
|
||||
Script done on Thu 24 Feb 2022 10:41:07 AM CST
|
84
CPE455/Quiz-Charlie/before.txt
Normal file
84
CPE455/Quiz-Charlie/before.txt
Normal file
@ -0,0 +1,84 @@
|
||||
Script started on Thu 24 Feb 2022 10:34:10 AM CST
|
||||
[?1034hbash-4.2$ ./vulnerable_capp
|
||||
|
||||
Enter the password now: aaaa
|
||||
|
||||
Login failure
|
||||
|
||||
suppliedPassword: aaaa
|
||||
sizeof(suppliedPassword) = 20
|
||||
strlen(suppliedPassword) = 4
|
||||
correctPassword: aaaaabbbbbccccc
|
||||
sizeof(correctPassword) = 20
|
||||
strlen(correctPassword) = 15
|
||||
|
||||
bash-4.2$ ./vulnerable_capp
|
||||
|
||||
Enter the password now: aaaaabbbbbccccc
|
||||
|
||||
Login successful
|
||||
|
||||
suppliedPassword: aaaaabbbbbccccc
|
||||
sizeof(suppliedPassword) = 20
|
||||
strlen(suppliedPassword) = 15
|
||||
correctPassword: aaaaabbbbbccccc
|
||||
sizeof(correctPassword) = 20
|
||||
strlen(correctPassword) = 15
|
||||
|
||||
bash-4.2$ perl -e 'print "a"x90' | ./vulnerable_capp
|
||||
|
||||
Enter the password now:
|
||||
Login successful
|
||||
|
||||
suppliedPassword: aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
|
||||
sizeof(suppliedPassword) = 20
|
||||
strlen(suppliedPassword) = 90
|
||||
correctPassword: aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
|
||||
sizeof(correctPassword) = 20
|
||||
strlen(correctPassword) = 58
|
||||
|
||||
Segmentation fault
|
||||
bash-4.2$ ./protected_capp
|
||||
|
||||
Enter the password now: aaaa
|
||||
|
||||
Login failure
|
||||
|
||||
suppliedPassword: aaaa
|
||||
sizeof(suppliedPassword) = 20
|
||||
strlen(suppliedPassword) = 4
|
||||
correctPassword: aaaaabbbbbccccc
|
||||
sizeof(correctPassword) = 20
|
||||
strlen(correctPassword) = 15
|
||||
|
||||
bash-4.2$ ./protected_capp
|
||||
[C[C[C[C[C[C[C[C[C[Cperl -e 'print "a"x90' | ./vulnerable_capp
|
||||
[C[C[C[C[C[C[C[C[C[C[26P./protected_capp
|
||||
|
||||
Enter the password now: aaaaabbbbbccccc
|
||||
|
||||
Login successful
|
||||
|
||||
suppliedPassword: aaaaabbbbbccccc
|
||||
sizeof(suppliedPassword) = 20
|
||||
strlen(suppliedPassword) = 15
|
||||
correctPassword: aaaaabbbbbccccc
|
||||
sizeof(correctPassword) = 20
|
||||
strlen(correctPassword) = 15
|
||||
|
||||
bash-4.2$ perl -e 'print "a"x90' | ./protected_capp
|
||||
|
||||
Enter the password now:
|
||||
Login failure
|
||||
|
||||
suppliedPassword: aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
|
||||
sizeof(suppliedPassword) = 20
|
||||
strlen(suppliedPassword) = 90
|
||||
correctPassword: aaaaabbbbbccccc
|
||||
sizeof(correctPassword) = 20
|
||||
strlen(correctPassword) = 15
|
||||
|
||||
*** stack smashing detected ***: ./protected_capp terminated
|
||||
Segmentation fault
|
||||
bash-4.2$ exit
|
||||
exit
|
51
CPE455/Quiz-Charlie/capp.c
Normal file
51
CPE455/Quiz-Charlie/capp.c
Normal file
@ -0,0 +1,51 @@
|
||||
/*
|
||||
* capp.c
|
||||
*
|
||||
*/
|
||||
|
||||
#include <stdio.h>
|
||||
#include <string.h>
|
||||
#include <signal.h>
|
||||
|
||||
#define BUFFER_SIZE 20
|
||||
|
||||
int main()
|
||||
{
|
||||
char correctPassword[BUFFER_SIZE];
|
||||
char suppliedPassword[BUFFER_SIZE];
|
||||
|
||||
printf("\nEnter the password now: ");
|
||||
strncpy(correctPassword, "aaaaabbbbbccccc", BUFFER_SIZE);
|
||||
scanf("%19s", suppliedPassword);
|
||||
printf("\n");
|
||||
|
||||
if (strncmp(suppliedPassword, correctPassword, BUFFER_SIZE) == 0)
|
||||
{
|
||||
printf("Login successful\n\n");
|
||||
}
|
||||
else
|
||||
{
|
||||
printf("Login failure\n\n");
|
||||
}
|
||||
|
||||
printf("suppliedPassword: %s\n", suppliedPassword);
|
||||
printf("sizeof(suppliedPassword) = %d\n", sizeof(suppliedPassword));
|
||||
printf("strlen(suppliedPassword) = %d\n", strlen(suppliedPassword));
|
||||
|
||||
printf("correctPassword: %s\n", correctPassword);
|
||||
printf("sizeof(correctPassword) = %d\n", sizeof(correctPassword));
|
||||
printf("strlen(correctPassword) = %d\n", strlen(correctPassword));
|
||||
|
||||
char myArray[BUFFER_SIZE];
|
||||
scanf("%s", myArray);
|
||||
printf("myArray: %s\n", myArray);
|
||||
printf("sizeof(myArray) = %d\n", sizeof(myArray));
|
||||
printf("strlen(myArray) = %d\n", strlen(myArray));
|
||||
|
||||
printf("\n");
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
||||
|
18
CPE455/Quiz-Charlie/makefile
Normal file
18
CPE455/Quiz-Charlie/makefile
Normal file
@ -0,0 +1,18 @@
|
||||
#
|
||||
# capp.c makefile
|
||||
#
|
||||
|
||||
CC = gcc
|
||||
CFLAGS = -g
|
||||
|
||||
vulnerable_capp: capp.c
|
||||
$(CC) $(CFLAGS) -fno-stack-protector -z execstack capp.c -o vulnerable_capp
|
||||
|
||||
protected_capp: capp.c
|
||||
$(CC) $(CFLAGS) -fstack-protector -z noexecstack capp.c -o protected_capp
|
||||
|
||||
clean:
|
||||
rm vulnerable_capp protected_capp
|
||||
|
||||
|
||||
|
BIN
CPE455/Quiz-Charlie/myarray
Executable file
BIN
CPE455/Quiz-Charlie/myarray
Executable file
Binary file not shown.
BIN
CPE455/Quiz-Charlie/protected_capp
Executable file
BIN
CPE455/Quiz-Charlie/protected_capp
Executable file
Binary file not shown.
44
CPE455/Quiz-Charlie/result.txt
Normal file
44
CPE455/Quiz-Charlie/result.txt
Normal file
@ -0,0 +1,44 @@
|
||||
Script started on Thu 24 Feb 2022 10:42:50 AM CST
|
||||
[?1034hbash-4.2$ exitperl -e 'print "a"x90' | ./myarray
|
||||
[C[C[C[C[C[C[C[C[C[C./myarray [Kgcc -g -fno-stack-protector -z execstack capp.c -o myarray
|
||||
[C[C[C[C[C[C[C[C[C[Cscript after.txt[K
|
||||
[C[C[C[C[C[C[C[C[C[Cgcc -g -fno-stack-protector -z execstack capp.c -o myarray
|
||||
gcc: error: capp.c: No such file or directory
|
||||
gcc: fatal error: no input files
|
||||
compilation terminated.
|
||||
bash-4.2$ cd CPE[K[K[KQuiz-C
|
||||
bash: cd: Quiz-C: No such file or directory
|
||||
bash-4.2$ cd Quiz-Charlie/
|
||||
bash-4.2$ cd Quiz-Charlie/[Kgcc -g -fno-stack-protector -z execstack capp.c -o myarray
|
||||
bash-4.2$ gcc -g -fno-stack-protector -z execstack capp.c -o myarray
|
||||
[C[C[C[C[C[C[C[C[C[Ccd Quiz-Charlie/[K[Kgcc -g -fno-stack-protector -z execstack capp.c -o myarray
|
||||
[C[C[C[C[C[C[C[C[C[Cexit[Kperl -e 'print "a"x90' | ./myarray
|
||||
[C[C[C[C[C[C[C[C[C[C./myarray [K
|
||||
|
||||
Enter the password now: aaaaabbbbbcccccdddddeeeee
|
||||
|
||||
Login failure
|
||||
|
||||
suppliedPassword: aaaaabbbbbcccccdddd
|
||||
sizeof(suppliedPassword) = 20
|
||||
strlen(suppliedPassword) = 19
|
||||
correctPassword: aaaaabbbbbccccc
|
||||
sizeof(correctPassword) = 20
|
||||
strlen(correctPassword) = 15
|
||||
myArray: deeeee
|
||||
sizeof(myArray) = 20
|
||||
strlen(myArray) = 6
|
||||
|
||||
bash-4.2$ ./myarray gcc -g -fno-stack-protector -z execstack capp.c -o myarray
|
||||
[C[C[C[C[C[C[C[C[C[C./myarray [Kgcc -g -fno-stack-protector -z execstack capp.c -o myarray
|
||||
[C[C[C[C[C[C[C[C[C[Ccd Quiz-Charlie/[K[Kgcc -g -fno-stack-protector -z execstack capp.c -o myarray
|
||||
[C[C[C[C[C[C[C[C[C[Cexit[Kperl -e 'print "a"x90' | ./myarray
|
||||
|
||||
Enter the password now:
|
||||
Login failure
|
||||
|
||||
suppliedPassword: aaaaaaaaaaaaaaaaaaa
|
||||
sizeof(suppliedPassword) = 20
|
||||
strlen(suppliedPassword) = 19
|
||||
correctPassword: aaaaabbbbbccccc
|
||||
sizeof(correctPassword) = 20
|
BIN
CPE455/Quiz-Charlie/vulnerable_capp
Executable file
BIN
CPE455/Quiz-Charlie/vulnerable_capp
Executable file
Binary file not shown.
1
CPE455/alpha/.vscode/configurationCache.log
vendored
Normal file
1
CPE455/alpha/.vscode/configurationCache.log
vendored
Normal file
@ -0,0 +1 @@
|
||||
{"buildTargets":["clean","fixme","fortified","make","vulnerable"],"launchTargets":[],"customConfigurationProvider":{"workspaceBrowse":{"browsePath":[]},"fileIndex":[]}}
|
5
CPE455/alpha/.vscode/dryrun.log
vendored
Normal file
5
CPE455/alpha/.vscode/dryrun.log
vendored
Normal file
@ -0,0 +1,5 @@
|
||||
make --dry-run --keep-going --print-directory
|
||||
make: Entering directory `/home/student/anw0044/CPE455/alpha'
|
||||
make: `fixme' is up to date.
|
||||
make: Leaving directory `/home/student/anw0044/CPE455/alpha'
|
||||
|
3
CPE455/alpha/.vscode/settings.json
vendored
Normal file
3
CPE455/alpha/.vscode/settings.json
vendored
Normal file
@ -0,0 +1,3 @@
|
||||
{
|
||||
"makefile.extensionOutputFolder": "./.vscode"
|
||||
}
|
327
CPE455/alpha/.vscode/targets.log
vendored
Normal file
327
CPE455/alpha/.vscode/targets.log
vendored
Normal file
@ -0,0 +1,327 @@
|
||||
make all --print-data-base --no-builtin-variables --no-builtin-rules --question
|
||||
# GNU Make 3.82
|
||||
# Built for x86_64-redhat-linux-gnu
|
||||
# Copyright (C) 2010 Free Software Foundation, Inc.
|
||||
# License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>
|
||||
# This is free software: you are free to change and redistribute it.
|
||||
# There is NO WARRANTY, to the extent permitted by law.
|
||||
|
||||
# Make data base, printed on Tue Feb 15 13:54:28 2022
|
||||
|
||||
# Variables
|
||||
|
||||
# automatic
|
||||
<D = $(patsubst %/,%,$(dir $<))
|
||||
# automatic
|
||||
?F = $(notdir $?)
|
||||
# environment
|
||||
DESKTOP_SESSION = gnome-classic
|
||||
# default
|
||||
.SHELLFLAGS := -c
|
||||
# environment
|
||||
XDG_SESSION_TYPE = x11
|
||||
# environment
|
||||
VSCODE_LOG_NATIVE = false
|
||||
# environment
|
||||
MODULEPATH = /usr/share/Modules/modulefiles:/etc/modulefiles
|
||||
# automatic
|
||||
?D = $(patsubst %/,%,$(dir $?))
|
||||
# automatic
|
||||
@D = $(patsubst %/,%,$(dir $@))
|
||||
# environment
|
||||
XAUTHORITY = /run/gdm/auth-for-anw0044-pmYcFt/database
|
||||
# environment
|
||||
GDMSESSION = gnome-classic
|
||||
# environment
|
||||
XMODIFIERS = @im=ibus
|
||||
# makefile
|
||||
CURDIR := /home/student/anw0044/CPE455/alpha
|
||||
# makefile
|
||||
SHELL = /bin/sh
|
||||
# environment
|
||||
GDM_LANG = en_US.UTF-8
|
||||
# environment
|
||||
VSCODE_NLS_CONFIG = {"locale":"en-us","availableLanguages":{},"_languagePackSupport":true}
|
||||
# environment
|
||||
_ = /usr/bin/make
|
||||
# makefile (from `Makefile', line 10)
|
||||
CFLAGS = -g
|
||||
# environment
|
||||
ftp_proxy = http://172.21.0.47:3128/
|
||||
# environment
|
||||
LESSOPEN = ||/usr/bin/lesspipe.sh %s
|
||||
# makefile (from `Makefile', line 1)
|
||||
MAKEFILE_LIST := Makefile
|
||||
# automatic
|
||||
@F = $(notdir $@)
|
||||
# environment
|
||||
GIO_LAUNCHED_DESKTOP_FILE = /home/student/anw0044/Desktop/code.desktop
|
||||
# environment
|
||||
USERNAME = anw0044
|
||||
# environment
|
||||
XDG_DATA_DIRS = /home/student/anw0044/.local/share/flatpak/exports/share/:/var/lib/flatpak/exports/share/:/usr/local/share/:/usr/share/
|
||||
# environment
|
||||
DBUS_SESSION_BUS_ADDRESS = unix:abstract=/tmp/dbus-wNNfwQpyb2,guid=c935a732f53252293842ee20620c02bb
|
||||
# environment
|
||||
VSCODE_VERBOSE_LOGGING = true
|
||||
# environment
|
||||
VSCODE_IPC_HOOK_EXTHOST = /run/user/17833/vscode-ipc-32fa229d-a9df-4645-a585-8e3779e8a09b.sock
|
||||
# environment
|
||||
VSCODE_CWD = /home/student/anw0044
|
||||
# environment
|
||||
HISTCONTROL = ignoredups
|
||||
# environment
|
||||
PATH = /opt/Qt/5.12.4/gcc_64/bin/:/opt/Qt/Tools/QtCreator/bin:/opt/Qt/5.12.4/gcc_64/bin/:/opt/Qt/Tools/QtCreator/bin:/usr/local/bin:/usr/local/sbin:/usr/bin:/usr/sbin:/bin:/sbin
|
||||
# environment
|
||||
LM_LICENSE_FILE = 1717@lm1
|
||||
# environment
|
||||
SESSION_MANAGER = local/unix:@/tmp/.ICE-unix/26133,unix/unix:/tmp/.ICE-unix/26133
|
||||
# environment
|
||||
XDG_RUNTIME_DIR = /run/user/17833
|
||||
# environment
|
||||
BREAKPAD_DUMP_LOCATION = /home/student/anw0044/.config/Code/exthost Crash Reports
|
||||
# environment
|
||||
XDG_MENU_PREFIX = gnome-
|
||||
# environment
|
||||
VSCODE_LOG_STACK = false
|
||||
# environment
|
||||
VSCODE_CRASH_REPORTER_START_OPTIONS = {"companyName":"Microsoft","productName":"VSCode","submitURL":"appcenter://code?aid=fba07a4d-84bd-4fc8-a125-9640fc8ce171&uid=3e139751-5b03-4856-ae18-f236e55a02bc&iid=3e139751-5b03-4856-ae18-f236e55a02bc&sid=3e139751-5b03-4856-ae18-f236e55a02bc","uploadToServer":true}
|
||||
# default
|
||||
.FEATURES := target-specific order-only second-expansion else-if shortest-stem undefine oneshell archives jobserver check-symlink
|
||||
# environment
|
||||
XDG_SESSION_DESKTOP = gnome-classic
|
||||
# environment
|
||||
SSH_AUTH_SOCK = /run/user/17833/keyring/ssh
|
||||
# environment
|
||||
GIO_LAUNCHED_DESKTOP_FILE_PID = 28008
|
||||
# automatic
|
||||
%F = $(notdir $%)
|
||||
# environment
|
||||
DISPLAY = :0
|
||||
# environment
|
||||
IMSETTINGS_INTEGRATE_DESKTOP = yes
|
||||
# environment
|
||||
PWD = /home/student/anw0044/CPE455/alpha
|
||||
# environment
|
||||
XDG_SEAT = seat0
|
||||
# environment
|
||||
SSH_AGENT_PID = 26258
|
||||
# environment
|
||||
ORIGINAL_XDG_CURRENT_DESKTOP = GNOME-Classic:GNOME
|
||||
# environment
|
||||
VSCODE_AMD_ENTRYPOINT = vs/workbench/services/extensions/node/extensionHostProcess
|
||||
# environment
|
||||
HOME = /home/student/anw0044
|
||||
# environment
|
||||
LD_LIBRARY_PATH = /opt/Qt/5.12.4/gcc_64/lib:/opt/Qt/5.12.4/gcc_64/lib:
|
||||
# environment
|
||||
VSCODE_CODE_CACHE_PATH = /home/student/anw0044/.config/Code/CachedData/f80445acd5a3dadef24aa209168452a3d97cc326
|
||||
# environment
|
||||
LOGNAME = anw0044
|
||||
# environment
|
||||
APPLICATION_INSIGHTS_NO_DIAGNOSTIC_CHANNEL = 1
|
||||
# environment
|
||||
VSCODE_HANDLES_UNCAUGHT_ERRORS = true
|
||||
# default
|
||||
MAKE_VERSION := 3.82
|
||||
# environment
|
||||
GNOME_DESKTOP_SESSION_ID = this-is-deprecated
|
||||
# automatic
|
||||
^D = $(patsubst %/,%,$(dir $^))
|
||||
# environment
|
||||
HOSTNAME = eb216-03
|
||||
# environment
|
||||
XDG_VTNR = 1
|
||||
# environment
|
||||
MAKELEVEL := 0
|
||||
# default
|
||||
MAKE = $(MAKE_COMMAND)
|
||||
# default
|
||||
MAKECMDGOALS := all
|
||||
# environment
|
||||
QT_IM_MODULE = ibus
|
||||
# environment
|
||||
SHLVL = 3
|
||||
# makefile (from `Makefile', line 9)
|
||||
CC = gcc
|
||||
# environment
|
||||
GNOME_SHELL_SESSION_MODE = classic
|
||||
# environment
|
||||
XDG_SESSION_ID = 797
|
||||
# environment
|
||||
USER = anw0044
|
||||
# makefile
|
||||
.DEFAULT_GOAL := fixme
|
||||
# environment
|
||||
XDG_CURRENT_DESKTOP = GNOME-Classic:GNOME
|
||||
# automatic
|
||||
%D = $(patsubst %/,%,$(dir $%))
|
||||
# environment
|
||||
define BASH_FUNC_module()
|
||||
() { eval `/usr/bin/modulecmd bash $*`
|
||||
}
|
||||
endef
|
||||
# default
|
||||
MAKE_COMMAND := make
|
||||
# default
|
||||
.VARIABLES :=
|
||||
# environment
|
||||
IMSETTINGS_MODULE = none
|
||||
# automatic
|
||||
*F = $(notdir $*)
|
||||
# environment
|
||||
VSCODE_IPC_HOOK = /run/user/17833/vscode-cca9002f-1.64.2-main.sock
|
||||
# makefile
|
||||
MAKEFLAGS = Rrqp
|
||||
# environment
|
||||
MFLAGS = -Rrqp
|
||||
# automatic
|
||||
*D = $(patsubst %/,%,$(dir $*))
|
||||
# environment
|
||||
MAIL = /var/spool/mail/anw0044
|
||||
# environment
|
||||
LOADEDMODULES =
|
||||
# automatic
|
||||
+D = $(patsubst %/,%,$(dir $+))
|
||||
# environment
|
||||
GDK_BACKEND = x11
|
||||
# environment
|
||||
MODULESHOME = /usr/share/Modules
|
||||
# automatic
|
||||
+F = $(notdir $+)
|
||||
# environment
|
||||
NO_AT_BRIDGE = 1
|
||||
# environment
|
||||
VSCODE_PIPE_LOGGING = true
|
||||
# default
|
||||
MAKEFILES :=
|
||||
# automatic
|
||||
<F = $(notdir $<)
|
||||
# environment
|
||||
https_proxy = https://172.21.0.47:3128/
|
||||
# environment
|
||||
ELECTRON_RUN_AS_NODE = 1
|
||||
# environment
|
||||
LC_ALL = C
|
||||
# automatic
|
||||
^F = $(notdir $^)
|
||||
# default
|
||||
SUFFIXES :=
|
||||
# environment
|
||||
HISTSIZE = 1000
|
||||
# environment
|
||||
CHROME_DESKTOP = code-url-handler.desktop
|
||||
# environment
|
||||
WINDOWPATH = 1:1
|
||||
# default
|
||||
.INCLUDE_DIRS = /usr/include /usr/local/include /usr/include
|
||||
# default
|
||||
.RECIPEPREFIX :=
|
||||
# environment
|
||||
http_proxy = http://172.21.0.47:3128/
|
||||
# environment
|
||||
LANG = C
|
||||
# environment
|
||||
TERM = dumb
|
||||
# environment
|
||||
VSCODE_PID = 28008
|
||||
# variable set hash-table stats:
|
||||
# Load=106/1024=10%, Rehash=0, Collisions=13/128=10%
|
||||
|
||||
# Pattern-specific Variable Values
|
||||
|
||||
# No pattern-specific variable values.
|
||||
|
||||
# Directories
|
||||
|
||||
# . (device 40, inode 196412580): 8 files, no impossibilities.
|
||||
|
||||
# 8 files, no impossibilities in 1 directories.
|
||||
|
||||
# Implicit Rules
|
||||
|
||||
# No implicit rules.
|
||||
|
||||
# Files
|
||||
|
||||
# Not a target:
|
||||
all:
|
||||
# Command line target.
|
||||
# Implicit rule search has been done.
|
||||
# File does not exist.
|
||||
# File has not been updated.
|
||||
|
||||
# Not a target:
|
||||
fixme.c:
|
||||
# Implicit rule search has not been done.
|
||||
# Modification time never checked.
|
||||
# File has not been updated.
|
||||
|
||||
# Not a target:
|
||||
.SUFFIXES:
|
||||
# Implicit rule search has not been done.
|
||||
# Modification time never checked.
|
||||
# File has not been updated.
|
||||
|
||||
# Not a target:
|
||||
Makefile:
|
||||
# Implicit rule search has been done.
|
||||
# Last modified 2022-02-15 10:22:48.042964896
|
||||
# File has been updated.
|
||||
# Successfully updated.
|
||||
|
||||
# Not a target:
|
||||
.DEFAULT:
|
||||
# Implicit rule search has not been done.
|
||||
# Modification time never checked.
|
||||
# File has not been updated.
|
||||
|
||||
vulnerable: fixme.c
|
||||
# Implicit rule search has not been done.
|
||||
# Modification time never checked.
|
||||
# File has not been updated.
|
||||
# recipe to execute (from `Makefile', line 16):
|
||||
$(CC) $(CFLAGS) fixme.c -o vulnerable
|
||||
|
||||
clean:
|
||||
# Implicit rule search has not been done.
|
||||
# Modification time never checked.
|
||||
# File has not been updated.
|
||||
# recipe to execute (from `Makefile', line 22):
|
||||
rm vulnerable fortified fixme
|
||||
|
||||
fortified: fixme.c
|
||||
|
||||
make: *** No rule to make target `all'. Stop.
|
||||
|
||||
# Implicit rule search has not been done.
|
||||
# Modification time never checked.
|
||||
# File has not been updated.
|
||||
# recipe to execute (from `Makefile', line 19):
|
||||
$(CC) $(CFLAGS) -D_FORTIFY_SOURCE=1 -O1 fixme.c -o fortified
|
||||
|
||||
fixme: fixme.c
|
||||
# Implicit rule search has not been done.
|
||||
# Modification time never checked.
|
||||
# File has not been updated.
|
||||
# recipe to execute (from `Makefile', line 13):
|
||||
$(CC) fixme.c -o fixme
|
||||
|
||||
# files hash-table stats:
|
||||
# Load=9/1024=1%, Rehash=0, Collisions=0/22=0%
|
||||
# VPATH Search Paths
|
||||
|
||||
# No `vpath' search paths.
|
||||
|
||||
# No general (`VPATH' variable) search path.
|
||||
|
||||
# # of strings in strcache: 0 / lookups = 28 / hits = 28
|
||||
# # of strcache buffers: 1 (* 8176 B/buffer = 8176 B)
|
||||
# strcache used: total = 0 (118) / max = 0 / min = 8176 / avg = 0
|
||||
# strcache free: total = 0 (8058) / max = 0 / min = 8176 / avg = 0
|
||||
|
||||
# strcache hash-table stats:
|
||||
# Load=14/8192=0%, Rehash=0, Collisions=1/28=4%
|
||||
# Finished Make data base on Tue Feb 15 13:54:28 2022
|
||||
|
||||
|
22
CPE455/alpha/Makefile
Normal file
22
CPE455/alpha/Makefile
Normal file
@ -0,0 +1,22 @@
|
||||
#
|
||||
# alpha makefile
|
||||
#
|
||||
# type 'make fixme' to build the vulnerable version of the program without GDB support
|
||||
# type 'make vulnerable' to build the vulnerable version of the program
|
||||
# type 'make fortified' to build the fortified version of the program
|
||||
#
|
||||
|
||||
CC = gcc
|
||||
CFLAGS = -g
|
||||
|
||||
fixme: fixme.c
|
||||
$(CC) fixme.c -o fixme
|
||||
|
||||
vulnerable: fixme.c
|
||||
$(CC) $(CFLAGS) fixme.c -o vulnerable
|
||||
|
||||
fortified: fixme.c
|
||||
$(CC) $(CFLAGS) -D_FORTIFY_SOURCE=1 -O1 fixme.c -o fortified
|
||||
|
||||
clean:
|
||||
rm vulnerable fortified fixme
|
BIN
CPE455/alpha/fixme
Executable file
BIN
CPE455/alpha/fixme
Executable file
Binary file not shown.
16
CPE455/alpha/fixme.c
Normal file
16
CPE455/alpha/fixme.c
Normal file
@ -0,0 +1,16 @@
|
||||
/*
|
||||
* fixme.c
|
||||
*/
|
||||
|
||||
#include <stdio.h>
|
||||
#include <string.h>
|
||||
|
||||
int main(int argc, char* argv[ ])
|
||||
{
|
||||
char buffer[5];
|
||||
|
||||
strcpy(buffer, argv[1]);
|
||||
puts(buffer);
|
||||
|
||||
return 0;
|
||||
}
|
BIN
CPE455/alpha/fortified
Executable file
BIN
CPE455/alpha/fortified
Executable file
Binary file not shown.
BIN
CPE455/alpha/vulnerable
Executable file
BIN
CPE455/alpha/vulnerable
Executable file
Binary file not shown.
7
CPE455/day8/Makefile
Normal file
7
CPE455/day8/Makefile
Normal file
@ -0,0 +1,7 @@
|
||||
CC = gcc
|
||||
CFLAGS = -Wall
|
||||
|
||||
hello: helloworld.o hellofunction.o
|
||||
$(CC) $(CFLAGS) helloworld.o hellofunction.o -o hello
|
||||
|
||||
hellorun.o: hello
|
1
CPE455/day8/hellofunction.c
Normal file
1
CPE455/day8/hellofunction.c
Normal file
@ -0,0 +1 @@
|
||||
void greetMe(void);
|
3
CPE455/day8/hellorun.c
Normal file
3
CPE455/day8/hellorun.c
Normal file
@ -0,0 +1,3 @@
|
||||
int main(){
|
||||
greetMe();
|
||||
}
|
3
CPE455/day8/helloworld.c
Normal file
3
CPE455/day8/helloworld.c
Normal file
@ -0,0 +1,3 @@
|
||||
void greetMe(){
|
||||
printf("Hello, World!/n");
|
||||
}
|
1
CPE455/gameproject/.svn/entries
Normal file
1
CPE455/gameproject/.svn/entries
Normal file
@ -0,0 +1 @@
|
||||
12
|
1
CPE455/gameproject/.svn/format
Normal file
1
CPE455/gameproject/.svn/format
Normal file
@ -0,0 +1 @@
|
||||
12
|
@ -0,0 +1,16 @@
|
||||
M 12 40
|
||||
########################################
|
||||
# # # #
|
||||
# # # #
|
||||
# # # #
|
||||
# # ###### ############ ### #
|
||||
# # # # # #
|
||||
# # # # # #
|
||||
# # ###### #### ########## #
|
||||
# # # #
|
||||
# # # #
|
||||
# # # #
|
||||
########################################
|
||||
Z 8 10
|
||||
P 2 10
|
||||
T 8 15
|
@ -0,0 +1,52 @@
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <stdint.h>
|
||||
#include <string.h>
|
||||
|
||||
int main(int argc, char *argv[])
|
||||
{
|
||||
printf("Hello, World!\n");
|
||||
FILE* mazeFile; /* Maze Input File */
|
||||
char c; /* Holds character for line input from file */
|
||||
int row; /* Holds number of rows for input from file */
|
||||
int col; /* Holds number of cols for input from file */
|
||||
char shold[2000]; /* Holds lines for input from file */
|
||||
if (argc != 2)
|
||||
{
|
||||
printf("An error has ocurred no input file was given.\n");
|
||||
return 1;
|
||||
}
|
||||
mazeFile = fopen(argv[1], "r");
|
||||
if (mazeFile == NULL)
|
||||
{
|
||||
printf("Error in opening the file.\n");
|
||||
return 1;
|
||||
}
|
||||
fgets(shold, 2, mazeFile);
|
||||
while ( !feof(mazeFile) )
|
||||
{
|
||||
switch(shold[0])
|
||||
{
|
||||
case 'M':
|
||||
printf ("Maze encountered\n");
|
||||
fscanf(mazeFile, "%d %d", &row, &col);
|
||||
break;
|
||||
default:
|
||||
printf ("Basic error\n");
|
||||
break;
|
||||
};
|
||||
fgets(shold, 2, mazeFile);
|
||||
if (feof(mazeFile))
|
||||
{
|
||||
break;
|
||||
}
|
||||
}
|
||||
fclose(mazeFile);
|
||||
printf ("So this line won't print without an argument.\n");
|
||||
|
||||
// possible dynamic allocation of 2D array
|
||||
char* ptr = malloc((row * col) * sizeof(char));
|
||||
|
||||
|
||||
return 0;
|
||||
}
|
@ -0,0 +1,290 @@
|
||||
#include <ncurses.h>
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <stdint.h>
|
||||
#include <string.h>
|
||||
#include <unistd.h>
|
||||
|
||||
WINDOW *initscr(void);
|
||||
int start_color(void);
|
||||
int cbreak(void);
|
||||
int noecho(void);
|
||||
int nodelay(WINDOW *win, bool bf);
|
||||
int keypad(WINDOW *win, bool bf);
|
||||
int curs_set(int visibility);
|
||||
/*void getmaxyx(WINDOW *win, int y, int x); */
|
||||
int init_pair(short pair, short f, short b);
|
||||
/*int attron(int attrs);
|
||||
int COLOR_PAIR(int n);
|
||||
int wmove(WINDOW *win, int y, int x);
|
||||
nothing*/
|
||||
|
||||
int main(int argc, char *argv[])
|
||||
{
|
||||
FILE* mazeFile; /* Maze Input File */
|
||||
unsigned int row = 0; /* Holds number of rows for input from file */
|
||||
unsigned int col = 0; /* Holds number of cols for input from file */
|
||||
unsigned int pcol = 0;
|
||||
unsigned int prow = 0;
|
||||
char shold[2000]; /* Holds lines for input from file */
|
||||
unsigned int l = 0;
|
||||
unsigned int k = 0;
|
||||
char **maze; /* array for maze */
|
||||
unsigned int c1 = 0;
|
||||
unsigned int c2 = 0;
|
||||
unsigned int i = 0;
|
||||
unsigned int j = 0;
|
||||
unsigned int x = 0;
|
||||
unsigned int y = 0;
|
||||
int deltaRow, deltaCol; /* Change in row-column position based on key input */
|
||||
unsigned int score = 0;
|
||||
|
||||
|
||||
WINDOW* W = 0; /* Curses Window handle */
|
||||
int rowLimit,colLimit; /* Num of rows and columns in terminal window */
|
||||
/* Initialize ncurses -- you will want to use the following settings */
|
||||
|
||||
|
||||
|
||||
/* check to see if argument */
|
||||
if (argc != 2)
|
||||
{
|
||||
/*printw("An error has ocurred no input file was given.\n");*/
|
||||
return 1;
|
||||
}
|
||||
mazeFile = fopen(argv[1], "r");
|
||||
if (mazeFile == NULL)
|
||||
{
|
||||
/*printw("Error in opening the file.\n");*/
|
||||
return 1;
|
||||
}
|
||||
fgets(shold, 2, mazeFile);
|
||||
while ( feof(mazeFile) == 0 )
|
||||
{
|
||||
|
||||
|
||||
switch(shold[0])
|
||||
{
|
||||
case 'M':
|
||||
/* read array values, dynamically allocate array */
|
||||
fscanf(mazeFile, "%3u %3u", &row, &col);
|
||||
maze = (char**)malloc(row * 8);
|
||||
for(i = (unsigned) 0; i < row; i++)
|
||||
{
|
||||
if (maze==NULL)
|
||||
{
|
||||
break;
|
||||
}
|
||||
maze[i] = malloc(col * 8);
|
||||
}
|
||||
for (x = (unsigned) 0; x < row; x++)
|
||||
{
|
||||
for (j = (unsigned) 0; j < col; j++)
|
||||
{
|
||||
fscanf(mazeFile, "%c", &maze[x][j]);
|
||||
if (maze[x][j] == '\n')
|
||||
{
|
||||
fscanf(mazeFile, "%c", &maze[x][j]);
|
||||
}
|
||||
}
|
||||
}
|
||||
break;
|
||||
case 'P':
|
||||
fscanf(mazeFile, "%3u %3u", &k, &l);
|
||||
/* check maze bounds */
|
||||
if(k>= row || l>= col){
|
||||
break;
|
||||
}
|
||||
maze[k][l] = 'P';
|
||||
break;
|
||||
|
||||
case 'Z':
|
||||
fscanf(mazeFile, "%3u %3u", &k, &l);
|
||||
/* check maze bounds */
|
||||
if(k>= row || l>= col){
|
||||
break;
|
||||
}
|
||||
maze[k][l] = 'Z';
|
||||
break;
|
||||
|
||||
case 'T':
|
||||
fscanf(mazeFile, "%3u %3u", &k, &l);
|
||||
/* check maze bounds */
|
||||
if(k>= row || l>= col){
|
||||
break;
|
||||
}
|
||||
maze[k][l] = 'T';
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
};
|
||||
fgets(shold, 2, mazeFile);
|
||||
if (feof(mazeFile) != 0)
|
||||
{
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
fclose(mazeFile);
|
||||
W = initscr(); /* Determine terminal type and initialize curses data structures */
|
||||
start_color(); /* Enable use of color with ncurses */
|
||||
cbreak(); /* Disable line buffering and make char inputs immediately accessible to program */
|
||||
noecho(); /* Disable echo printing of chars type by user */
|
||||
nodelay(W, true); /* Make getch( ) a non-blocking call */
|
||||
keypad(W, true); /* Enable keypad and arrow keys */
|
||||
curs_set(0); /* Set cursor to be invisible - 0 */
|
||||
getmaxyx(W, rowLimit, colLimit); /* Query terminal dimensions */
|
||||
|
||||
/* Define color pallete foreground-background color pairs */
|
||||
/* PairID#, Foreground Color, Background Color */
|
||||
/* Foreground color == Background color ==> solid color block */
|
||||
init_pair(1, COLOR_BLACK, COLOR_BLACK); /* Black text on Black background */
|
||||
init_pair(2, COLOR_GREEN, COLOR_GREEN); /* Red text on Black background */
|
||||
init_pair(3, COLOR_WHITE, COLOR_WHITE); /* White text on Blue background */
|
||||
init_pair(4, COLOR_RED, COLOR_RED); /* Yellow text on Yellow background */
|
||||
init_pair(5, COLOR_YELLOW, COLOR_YELLOW); /* Yellow text on Yellow background */
|
||||
|
||||
|
||||
for (c1 =(unsigned)0; c1 < row; c1++)
|
||||
{
|
||||
/*printw ("\n");*/
|
||||
for (c2=(unsigned)0; c2 < col; c2++)
|
||||
{
|
||||
if (maze==NULL)
|
||||
{
|
||||
break;
|
||||
}
|
||||
switch(maze[c1][c2])
|
||||
{
|
||||
case '#':
|
||||
/* read array values, dynamically allocate array*/
|
||||
attron(COLOR_PAIR(2));
|
||||
wmove(W, c1,c2);
|
||||
waddch(W,'#');
|
||||
attroff(COLOR_PAIR(2));
|
||||
break;
|
||||
case ' ':
|
||||
wmove(W, c1,c2);
|
||||
waddch(W,' ');
|
||||
break;
|
||||
case 'P':
|
||||
wmove(W, c1,c2);
|
||||
waddch(W,'P');
|
||||
prow=c1;
|
||||
pcol=c2;
|
||||
break;
|
||||
case 'Z':
|
||||
attron(COLOR_PAIR(4));
|
||||
wmove(W, c1,c2);
|
||||
waddch(W,'Z');
|
||||
attroff(COLOR_PAIR(4));
|
||||
break;
|
||||
case 'T':
|
||||
attron(COLOR_PAIR(5));
|
||||
wmove(W, c1,c2);
|
||||
waddch(W,'T');
|
||||
attroff(COLOR_PAIR(5));
|
||||
break;
|
||||
|
||||
default:
|
||||
break;
|
||||
};
|
||||
/*printw("%c", maze[c1][c2]);*/
|
||||
}
|
||||
}
|
||||
|
||||
deltaRow =0;
|
||||
deltaCol =0;
|
||||
|
||||
|
||||
|
||||
do{
|
||||
|
||||
int kb = wgetch(W); /* Grab keypress */
|
||||
|
||||
/* Support player movement via WASD, IJKL, and arrow keys */
|
||||
if (kb == (int)'a' || kb == (int)'j' || kb == KEY_LEFT)
|
||||
{
|
||||
/* Move Left */
|
||||
deltaRow =0;
|
||||
deltaCol =-1;
|
||||
}
|
||||
else if (kb == (int)'d' || kb == (int)'l' || kb == KEY_RIGHT)
|
||||
{
|
||||
/* Move Right */
|
||||
deltaRow =0;
|
||||
deltaCol =1;
|
||||
}
|
||||
else if (kb == (int)'w' || kb == (int)'i' || kb == KEY_UP)
|
||||
{
|
||||
/* Move Up */
|
||||
deltaRow =-1;
|
||||
deltaCol =0;
|
||||
}
|
||||
else if (kb == (int)'s' || kb == (int)'k' || kb == KEY_DOWN)
|
||||
{
|
||||
/* Move Down */
|
||||
deltaRow =1;
|
||||
deltaCol =0;
|
||||
}
|
||||
else if (kb == (int)'q')
|
||||
{
|
||||
/* q to exit player movement loop */
|
||||
break;
|
||||
};
|
||||
|
||||
attron(COLOR_PAIR(1));
|
||||
wmove(W,prow,pcol);
|
||||
waddch(W, 'P');
|
||||
attroff(COLOR_PAIR(1));
|
||||
|
||||
if(((int)(prow + deltaRow) > row) || ((int)(pcol + deltaCol) > col)){
|
||||
deltaRow = 0;
|
||||
deltaCol = 0;
|
||||
}
|
||||
if(maze[(int)(prow + deltaRow)][(int)(pcol + deltaCol)] == '#'){
|
||||
prow = prow;
|
||||
pcol = pcol;
|
||||
}
|
||||
else{
|
||||
prow = prow + deltaRow;
|
||||
pcol = pcol + deltaCol;
|
||||
}
|
||||
if(maze[prow][pcol] == 'T')
|
||||
{
|
||||
maze[prow][pcol] = ' ';
|
||||
score++;
|
||||
}
|
||||
if(maze[prow][pcol] == 'Z')
|
||||
{
|
||||
break;
|
||||
}
|
||||
|
||||
attron(COLOR_PAIR(3));
|
||||
wmove(W,prow,pcol);
|
||||
waddch(W, 'P');
|
||||
attroff(COLOR_PAIR(3));
|
||||
|
||||
deltaRow =0;
|
||||
deltaCol =0;
|
||||
|
||||
wrefresh(W); /* Refresh ncurses window */
|
||||
|
||||
} while (true); /* Repeat until user selects q to quit */
|
||||
|
||||
/* free the memory */
|
||||
for(y = (unsigned)0; y < row; y++)
|
||||
{
|
||||
free(maze[y]);
|
||||
maze[y] = NULL;
|
||||
}
|
||||
if(maze != NULL){
|
||||
free(maze);
|
||||
}
|
||||
/*printw("\n");*/
|
||||
endwin();
|
||||
printf("Score = %u\n", score);
|
||||
return 0;
|
||||
}
|
||||
|
@ -0,0 +1,283 @@
|
||||
#include <ncurses.h>
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <stdint.h>
|
||||
#include <string.h>
|
||||
#include <unistd.h>
|
||||
|
||||
|
||||
/* LDRA_EXCLUDE RULE VIOLATION <justification start> FALSE POSITIVE -- why <justification end> */
|
||||
|
||||
int main(int argc, char *argv[])
|
||||
{
|
||||
FILE* mazeFile; /* Maze Input File */
|
||||
unsigned int row = 0; /* Holds number of rows for input from file */
|
||||
unsigned int col = 0; /* Holds number of cols for input from file */
|
||||
unsigned int pcol = 0;
|
||||
unsigned int prow = 0;
|
||||
char shold[2000]; /* Holds lines for input from file */
|
||||
unsigned int l = 0;
|
||||
unsigned int k = 0;
|
||||
char **maze; /* array for maze */
|
||||
unsigned int c1 = 0;
|
||||
unsigned int c2 = 0;
|
||||
unsigned int i = 0;
|
||||
unsigned int j = 0;
|
||||
unsigned int x = 0;
|
||||
unsigned int y = 0;
|
||||
int deltaRow, deltaCol; /* Change in row-column position based on key input */
|
||||
unsigned int score = 0;
|
||||
|
||||
|
||||
WINDOW* W = 0; /* Curses Window handle */
|
||||
int rowLimit,colLimit; /* Num of rows and columns in terminal window */
|
||||
/* Initialize ncurses -- you will want to use the following settings */
|
||||
|
||||
|
||||
|
||||
/* check to see if argument */
|
||||
if (argc != 2)
|
||||
{
|
||||
/*printw("An error has ocurred no input file was given.\n");*/
|
||||
return 1;
|
||||
}
|
||||
mazeFile = fopen(argv[1], "r");
|
||||
if (mazeFile == NULL)
|
||||
{
|
||||
/*printw("Error in opening the file.\n");*/
|
||||
return 1;
|
||||
}
|
||||
fgets(shold, 2, mazeFile);
|
||||
while ( feof(mazeFile) == 0 )
|
||||
{
|
||||
|
||||
|
||||
switch(shold[0])
|
||||
{
|
||||
case 'M':
|
||||
/* read array values, dynamically allocate array */
|
||||
fscanf(mazeFile, "%3u %3u", &row, &col);
|
||||
maze = (char**)malloc(row * 8); /* */
|
||||
for(i = (unsigned) 0; i < row; i++)
|
||||
{
|
||||
if (maze==NULL)
|
||||
{
|
||||
break;
|
||||
}
|
||||
maze[i] = malloc(col * 8);
|
||||
}
|
||||
for (x = (unsigned) 0; x < row; x++)
|
||||
{
|
||||
for (j = (unsigned) 0; j < col; j++)
|
||||
{
|
||||
fscanf(mazeFile, "%c", &maze[x][j]);
|
||||
if (maze[x][j] == '\n')
|
||||
{
|
||||
fscanf(mazeFile, "%c", &maze[x][j]);
|
||||
}
|
||||
}
|
||||
}
|
||||
break;
|
||||
case 'P':
|
||||
fscanf(mazeFile, "%3u %3u", &k, &l);
|
||||
/* check maze bounds */
|
||||
if(k>= row || l>= col){
|
||||
break;
|
||||
}
|
||||
maze[k][l] = 'P';
|
||||
break;
|
||||
|
||||
case 'Z':
|
||||
fscanf(mazeFile, "%3u %3u", &k, &l);
|
||||
/* check maze bounds */
|
||||
if(k>= row || l>= col){
|
||||
break;
|
||||
}
|
||||
maze[k][l] = 'Z';
|
||||
break;
|
||||
|
||||
case 'T':
|
||||
fscanf(mazeFile, "%3u %3u", &k, &l);
|
||||
/* check maze bounds */
|
||||
if(k>= row || l>= col){
|
||||
break;
|
||||
}
|
||||
maze[k][l] = 'T';
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
};
|
||||
fgets(shold, 2, mazeFile);
|
||||
if (feof(mazeFile) != 0)
|
||||
{
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
fclose(mazeFile);
|
||||
/* LDRA_EXCLUDE 170 S <justification start> FALSE POSITIVE -- why <justification end> */
|
||||
W = initscr(); /* Determine terminal type and initialize curses data structures */
|
||||
start_color(); /* Enable use of color with ncurses */
|
||||
cbreak(); /* Disable line buffering and make char inputs immediately accessible to program */
|
||||
noecho(); /* Disable echo printing of chars type by user */
|
||||
if (W == NULL)
|
||||
{
|
||||
return 0;
|
||||
}
|
||||
nodelay(W, true); /* Make getch( ) a non-blocking call */
|
||||
keypad(W, true); /* Enable keypad and arrow keys */
|
||||
curs_set(0); /* Set cursor to be invisible - 0 */
|
||||
getmaxyx(W, rowLimit, colLimit); /* Query terminal dimensions */
|
||||
|
||||
/* Define color pallete foreground-background color pairs */
|
||||
/* PairID#, Foreground Color, Background Color */
|
||||
/* Foreground color == Background color ==> solid color block */
|
||||
init_pair(1, COLOR_BLACK, COLOR_BLACK); /* Black text on Black background */
|
||||
init_pair(2, COLOR_GREEN, COLOR_GREEN); /* Red text on Black background */
|
||||
init_pair(3, COLOR_WHITE, COLOR_WHITE); /* White text on Blue background */
|
||||
init_pair(4, COLOR_RED, COLOR_RED); /* Yellow text on Yellow background */
|
||||
init_pair(5, COLOR_YELLOW, COLOR_YELLOW); /* Yellow text on Yellow background */
|
||||
|
||||
|
||||
for (c1 =(unsigned)0; c1 < row; c1++)
|
||||
{
|
||||
/*printw ("\n");*/
|
||||
for (c2=(unsigned)0; c2 < col; c2++)
|
||||
{
|
||||
if (maze==NULL)
|
||||
{
|
||||
break;
|
||||
}
|
||||
switch(maze[c1][c2])
|
||||
{
|
||||
case '#':
|
||||
/* read array values, dynamically allocate array*/
|
||||
attron(COLOR_PAIR(2));
|
||||
wmove(W, c1,c2);
|
||||
waddch(W,'#');
|
||||
attroff(COLOR_PAIR(2));
|
||||
break;
|
||||
case ' ':
|
||||
wmove(W, c1,c2);
|
||||
waddch(W,' ');
|
||||
break;
|
||||
case 'P':
|
||||
wmove(W, c1,c2);
|
||||
waddch(W,'P');
|
||||
prow=c1;
|
||||
pcol=c2;
|
||||
break;
|
||||
case 'Z':
|
||||
attron(COLOR_PAIR(4));
|
||||
wmove(W, c1,c2);
|
||||
waddch(W,'Z');
|
||||
attroff(COLOR_PAIR(4));
|
||||
break;
|
||||
case 'T':
|
||||
attron(COLOR_PAIR(5));
|
||||
wmove(W, c1,c2);
|
||||
waddch(W,'T');
|
||||
attroff(COLOR_PAIR(5));
|
||||
break;
|
||||
|
||||
default:
|
||||
break;
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
deltaRow =0;
|
||||
deltaCol =0;
|
||||
|
||||
|
||||
|
||||
do{
|
||||
|
||||
int kb = wgetch(W); /* Grab keypress */
|
||||
|
||||
/* Support player movement via WASD, IJKL, and arrow keys */
|
||||
if (kb == (int)'a' || kb == (int)'j' || kb == KEY_LEFT)
|
||||
{
|
||||
/* Move Left */
|
||||
deltaRow =0;
|
||||
deltaCol =-1;
|
||||
}
|
||||
else if (kb == (int)'d' || kb == (int)'l' || kb == KEY_RIGHT)
|
||||
{
|
||||
/* Move Right */
|
||||
deltaRow =0;
|
||||
deltaCol =1;
|
||||
}
|
||||
else if (kb == (int)'w' || kb == (int)'i' || kb == KEY_UP)
|
||||
{
|
||||
/* Move Up */
|
||||
deltaRow =-1;
|
||||
deltaCol =0;
|
||||
}
|
||||
else if (kb == (int)'s' || kb == (int)'k' || kb == KEY_DOWN)
|
||||
{
|
||||
/* Move Down */
|
||||
deltaRow =1;
|
||||
deltaCol =0;
|
||||
}
|
||||
else if (kb == (int)'q')
|
||||
{
|
||||
/* q to exit player movement loop */
|
||||
break;
|
||||
};
|
||||
|
||||
attron(COLOR_PAIR(1));
|
||||
wmove(W,prow,pcol);
|
||||
waddch(W, 'P');
|
||||
attroff(COLOR_PAIR(1));
|
||||
|
||||
if(((prow + (unsigned int)deltaRow) > row) || ((pcol + (unsigned int)deltaCol) > col)){
|
||||
deltaRow = 0;
|
||||
deltaCol = 0;
|
||||
}
|
||||
if(maze[((int)prow + deltaRow)][((int)pcol + deltaCol)] == '#'){
|
||||
prow = prow;
|
||||
pcol = pcol;
|
||||
}
|
||||
else{
|
||||
prow = prow + (unsigned int)deltaRow;
|
||||
pcol = pcol + (unsigned int)deltaCol;
|
||||
}
|
||||
if(maze[prow][pcol] == 'T')
|
||||
{
|
||||
maze[prow][pcol] = ' ';
|
||||
score++;
|
||||
}
|
||||
if(maze[prow][pcol] == 'Z')
|
||||
{
|
||||
break;
|
||||
}
|
||||
|
||||
attron(COLOR_PAIR(3));
|
||||
wmove(W,prow,pcol);
|
||||
waddch(W, 'P');
|
||||
attroff(COLOR_PAIR(3));
|
||||
|
||||
deltaRow =0;
|
||||
deltaCol =0;
|
||||
|
||||
wrefresh(W); /* Refresh ncurses window */
|
||||
|
||||
} while (true); /* Repeat until user selects q to quit */
|
||||
|
||||
/* free the memory */
|
||||
for(y = (unsigned)0; y < row; y++)
|
||||
{
|
||||
free(maze[y]);
|
||||
maze[y] = NULL;
|
||||
}
|
||||
if(maze != NULL){
|
||||
free(maze);
|
||||
}
|
||||
/*printw("\n");*/
|
||||
endwin();
|
||||
printf("Score = %u\n", score);
|
||||
return 0;
|
||||
}
|
||||
|
@ -0,0 +1,429 @@
|
||||
#include <ncurses.h>
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <stdint.h>
|
||||
#include <string.h>
|
||||
#include <unistd.h>
|
||||
|
||||
extern WINDOW* initscr(void);
|
||||
extern int start_color(void);
|
||||
extern int noecho(void);
|
||||
|
||||
|
||||
/* LDRA_EXCLUDE RULE VIOLATION <justification start> FALSE POSITIVE -- why <justification end> */
|
||||
|
||||
int main(int argc, char *argv[])
|
||||
{
|
||||
FILE* mazeFile; /* Maze Input File */
|
||||
unsigned int row = 0; /* Holds number of rows for input from file */
|
||||
unsigned int col = 0; /* Holds number of cols for input from file */
|
||||
unsigned int pcol = 0;
|
||||
unsigned int prow = 0;
|
||||
char shold[2000]; /* Holds lines for input from file */
|
||||
unsigned int l = 0;
|
||||
unsigned int k = 0;
|
||||
char **maze; /* array for maze */
|
||||
unsigned int c1 = 0;
|
||||
unsigned int c2 = 0;
|
||||
unsigned int i = 0;
|
||||
unsigned int j = 0;
|
||||
unsigned int x = 0;
|
||||
unsigned int y = 0;
|
||||
int deltaRow, deltaCol; /* Change in row-column position based on key input */
|
||||
unsigned int score = 0;
|
||||
|
||||
|
||||
WINDOW* W = 0; /* Curses Window handle */
|
||||
unsigned int rowLimit,colLimit; /* Num of rows and columns in terminal window */
|
||||
/* Initialize ncurses -- you will want to use the following settings */
|
||||
|
||||
|
||||
|
||||
/* check to see if argument */
|
||||
if (argc != 2)
|
||||
{
|
||||
/*printw("An error has ocurred no input file was given.\n");*/
|
||||
return 1;
|
||||
}
|
||||
/* LDRA_EXCLUDE 44 S <justification start> FALSE POSITIVE -- Need function to open the file so must use bad one bc we cannot create one ourselves as we are not wizards <justification end> */
|
||||
mazeFile = fopen(argv[1], "r");
|
||||
if (mazeFile == NULL)
|
||||
{
|
||||
/*printw("Error in opening the file.\n");*/
|
||||
return 1;
|
||||
}
|
||||
/* LDRA_EXCLUDE 44 S <justification start> FALSE POSITIVE -- Need function to get first char from file and we must use bad one as we cannot make one ourselves <justification end> */
|
||||
fgets(shold, 2, mazeFile);
|
||||
while ( feof(mazeFile) == 0 )
|
||||
{
|
||||
|
||||
/* parse maze file,
|
||||
adding chars to array */
|
||||
switch(shold[0])
|
||||
{
|
||||
case 'M':
|
||||
/* read array values, dynamically allocate array */
|
||||
/* LDRA_EXCLUDE 44 S <justification start> FALSE POSITIVE -- Need function to scan content from file, have ensured we will not scan extra <justification end> */
|
||||
fscanf(mazeFile, "%3u %3u", &row, &col);
|
||||
/* LDRA_EXCLUDE 44 S <justification start> FALSE POSITIVE -- Need function to dynamially allocate memory cannot create own as we lack experience <justification end> */
|
||||
maze = (char**)malloc(row * 8); /* allocate memory for rows of array, 8 reperesents size of char* */
|
||||
for(i = (unsigned) 0; i < row; i++)
|
||||
{
|
||||
if (maze==NULL)
|
||||
{
|
||||
break;
|
||||
}
|
||||
/* LDRA_EXCLUDE 44 S <justification start> FALSE POSITIVE -- Need function to dynamially allocate memory cannot create own as we lack experience <justification end> */
|
||||
maze[i] = malloc(col * 8); /* allocate memory for collumns of array, 8 reperesents size of char* */
|
||||
}
|
||||
for (x = (unsigned) 0; x < row; x++)
|
||||
{
|
||||
for (j = (unsigned) 0; j < col; j++)
|
||||
{
|
||||
/* LDRA_EXCLUDE 44 S <justification start> FALSE POSITIVE -- Need function to scan content from file, have ensured we will not scan extra <justification end> */
|
||||
fscanf(mazeFile, "%c", &maze[x][j]);
|
||||
if (maze[x][j] == '\n')
|
||||
{
|
||||
/* LDRA_EXCLUDE 44 S <justification start> FALSE POSITIVE -- Need function to scan content from file, have ensured we will not scan extra <justification end> */
|
||||
fscanf(mazeFile, "%c", &maze[x][j]);
|
||||
}
|
||||
}
|
||||
}
|
||||
break;
|
||||
case 'P':
|
||||
/* LDRA_EXCLUDE 44 S <justification start> FALSE POSITIVE -- Need function to scan content from file, have ensured we will not scan extra <justification end> */
|
||||
fscanf(mazeFile, "%3u %3u", &k, &l);
|
||||
/* check maze bounds */
|
||||
if(k>= row || l>= col){
|
||||
break;
|
||||
}
|
||||
maze[k][l] = 'P';
|
||||
break;
|
||||
|
||||
case 'Z':
|
||||
/* LDRA_EXCLUDE 44 S <justification start> FALSE POSITIVE -- Need function to scan content from file, have ensured we will not scan extra <justification end> */
|
||||
fscanf(mazeFile, "%3u %3u", &k, &l);
|
||||
/* check maze bounds */
|
||||
if(k>= row || l>= col){
|
||||
break;
|
||||
}
|
||||
maze[k][l] = 'Z';
|
||||
break;
|
||||
|
||||
case 'T':
|
||||
/* LDRA_EXCLUDE 44 S <justification start> FALSE POSITIVE -- Need function to scan content from file, have ensured we will not scan extra <justification end> */
|
||||
fscanf(mazeFile, "%3u %3u", &k, &l);
|
||||
/* check maze bounds */
|
||||
if(k>= row || l>= col){
|
||||
break;
|
||||
}
|
||||
maze[k][l] = 'T';
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
};
|
||||
/* LDRA_EXCLUDE 44 S <justification start> FALSE POSITIVE -- Need function to scan content from file, have ensured we will not scan extra <justification end> */
|
||||
fgets(shold, 2, mazeFile);
|
||||
if (feof(mazeFile) != 0)
|
||||
{
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
rowLimit = row;
|
||||
colLimit = col;
|
||||
fclose(mazeFile);
|
||||
/* LDRA_EXCLUDE 170 S <justification start> FALSE POSITIVE -- needed for ncurses <justification end> */
|
||||
/* LDRA_EXCLUDE 496 S <justification start> FALSE POSITIVE -- needed for ncurses <justification end> */
|
||||
/* LDRA_EXCLUDE 41 D <justification start> FALSE POSITIVE -- needed for ncurses <justification end> */
|
||||
W = initscr(); /* Determine terminal type and initialize curses data structures */
|
||||
/* LDRA_EXCLUDE 170 S <justification start> FALSE POSITIVE -- needed for ncurses <justification end> */
|
||||
/* LDRA_EXCLUDE 496 S <justification start> FALSE POSITIVE -- needed for ncurses <justification end> */
|
||||
/* LDRA_EXCLUDE 41 D <justification start> FALSE POSITIVE -- needed for ncurses <justification end> */
|
||||
start_color(); /* Enable use of color with ncurses */
|
||||
/* LDRA_EXCLUDE 170 S <justification start> FALSE POSITIVE -- needed for ncurses <justification end> */
|
||||
/* LDRA_EXCLUDE 496 S <justification start> FALSE POSITIVE -- needed for ncurses <justification end> */
|
||||
/* LDRA_EXCLUDE 41 D <justification start> FALSE POSITIVE -- needed for ncurses <justification end> */
|
||||
cbreak(); /* Disable line buffering and make char inputs immediately accessible to program */
|
||||
/* LDRA_EXCLUDE 170 S <justification start> FALSE POSITIVE -- needed for ncurses <justification end> */
|
||||
/* LDRA_EXCLUDE 496 S <justification start> FALSE POSITIVE -- needed for ncurses <justification end> */
|
||||
/* LDRA_EXCLUDE 41 D <justification start> FALSE POSITIVE -- needed for ncurses <justification end> */
|
||||
noecho(); /* Disable echo printing of chars type by user */
|
||||
if (W == NULL)
|
||||
{
|
||||
return 0;
|
||||
}
|
||||
/* LDRA_EXCLUDE 170 S <justification start> FALSE POSITIVE -- needed for ncurses <justification end> */
|
||||
/* LDRA_EXCLUDE 496 S <justification start> FALSE POSITIVE -- needed for ncurses <justification end> */
|
||||
/* LDRA_EXCLUDE 41 D <justification start> FALSE POSITIVE -- needed for ncurses <justification end> */
|
||||
nodelay(W, true); /* Make getch( ) a non-blocking call */
|
||||
/* LDRA_EXCLUDE 170 S <justification start> FALSE POSITIVE -- needed for ncurses <justification end> */
|
||||
/* LDRA_EXCLUDE 496 S <justification start> FALSE POSITIVE -- needed for ncurses <justification end> */
|
||||
/* LDRA_EXCLUDE 41 D <justification start> FALSE POSITIVE -- needed for ncurses <justification end> */
|
||||
keypad(W, true); /* Enable keypad and arrow keys */
|
||||
/* LDRA_EXCLUDE 170 S <justification start> FALSE POSITIVE -- needed for ncurses <justification end> */
|
||||
/* LDRA_EXCLUDE 496 S <justification start> FALSE POSITIVE -- needed for ncurses <justification end> */
|
||||
/* LDRA_EXCLUDE 41 D <justification start> FALSE POSITIVE -- needed for ncurses <justification end> */
|
||||
curs_set(0); /* Set cursor to be invisible - 0 */
|
||||
/* LDRA_EXCLUDE 170 S <justification start> FALSE POSITIVE -- needed for ncurses <justification end> */
|
||||
/* LDRA_EXCLUDE 496 S <justification start> FALSE POSITIVE -- needed for ncurses <justification end> */
|
||||
/* LDRA_EXCLUDE 41 D <justification start> FALSE POSITIVE -- needed for ncurses <justification end> */
|
||||
getmaxyx(W, rowLimit, colLimit); /* Query terminal dimensions */
|
||||
|
||||
/* Define color pallete foreground-background color pairs */
|
||||
/* PairID#, Foreground Color, Background Color */
|
||||
/* Foreground color == Background color ==> solid color block */
|
||||
/* LDRA_EXCLUDE 170 S <justification start> FALSE POSITIVE -- needed for ncurses <justification end> */
|
||||
/* LDRA_EXCLUDE 496 S <justification start> FALSE POSITIVE -- needed for ncurses <justification end> */
|
||||
/* LDRA_EXCLUDE 41 D <justification start> FALSE POSITIVE -- needed for ncurses <justification end> */
|
||||
init_pair(1, COLOR_BLACK, COLOR_BLACK); /* Black text on Black background */
|
||||
/* LDRA_EXCLUDE 170 S <justification start> FALSE POSITIVE -- needed for ncurses <justification end> */
|
||||
/* LDRA_EXCLUDE 496 S <justification start> FALSE POSITIVE -- needed for ncurses <justification end> */
|
||||
init_pair(2, COLOR_GREEN, COLOR_GREEN); /* Red text on Black background */
|
||||
/* LDRA_EXCLUDE 170 S <justification start> FALSE POSITIVE -- needed for ncurses <justification end> */
|
||||
/* LDRA_EXCLUDE 496 S <justification start> FALSE POSITIVE -- needed for ncurses <justification end> */
|
||||
init_pair(3, COLOR_WHITE, COLOR_WHITE); /* White text on Blue background */
|
||||
/* LDRA_EXCLUDE 170 S <justification start> FALSE POSITIVE -- needed for ncurses <justification end> */
|
||||
/* LDRA_EXCLUDE 496 S <justification start> FALSE POSITIVE -- needed for ncurses <justification end> */
|
||||
init_pair(4, COLOR_RED, COLOR_RED); /* Yellow text on Yellow background */
|
||||
/* LDRA_EXCLUDE 170 S <justification start> FALSE POSITIVE -- needed for ncurses <justification end> */
|
||||
/* LDRA_EXCLUDE 496 S <justification start> FALSE POSITIVE -- needed for ncurses <justification end> */
|
||||
init_pair(5, COLOR_YELLOW, COLOR_YELLOW); /* Yellow text on Yellow background */
|
||||
|
||||
|
||||
for (c1 =(unsigned)0; c1 < row; c1++)
|
||||
{
|
||||
/*printw ("\n");*/
|
||||
for (c2=(unsigned)0; c2 < col; c2++)
|
||||
{
|
||||
if (maze==NULL)
|
||||
{
|
||||
break;
|
||||
}
|
||||
/* parse maze */
|
||||
switch(maze[c1][c2])
|
||||
{
|
||||
case '#':
|
||||
/* read array values, dynamically allocate array*/
|
||||
/* LDRA_EXCLUDE 170 S <justification start> FALSE POSITIVE -- needed for ncurses <justification end> */
|
||||
/* LDRA_EXCLUDE 170 S <justification start> FALSE POSITIVE -- needed for ncurses <justification end> */
|
||||
/* LDRA_EXCLUDE 496 S <justification start> FALSE POSITIVE -- needed for ncurses <justification end> */
|
||||
/* LDRA_EXCLUDE 496 S <justification start> FALSE POSITIVE -- needed for ncurses <justification end> */
|
||||
/* LDRA_EXCLUDE 41 D <justification start> FALSE POSITIVE -- needed for ncurses <justification end> */
|
||||
/* LDRA_EXCLUDE 41 D <justification start> FALSE POSITIVE -- needed for ncurses <justification end> */
|
||||
attron(COLOR_PAIR(2));
|
||||
/* LDRA_EXCLUDE 170 S <justification start> FALSE POSITIVE -- needed for ncurses <justification end> */
|
||||
/* LDRA_EXCLUDE 496 S <justification start> FALSE POSITIVE -- needed for ncurses <justification end> */
|
||||
wmove(W, c1,c2);
|
||||
/* LDRA_EXCLUDE 170 S <justification start> FALSE POSITIVE -- needed for ncurses <justification end> */
|
||||
/* LDRA_EXCLUDE 496 S <justification start> FALSE POSITIVE -- needed for ncurses <justification end> */
|
||||
waddch(W,'#');
|
||||
/* LDRA_EXCLUDE 170 S <justification start> FALSE POSITIVE -- needed for ncurses <justification end> */
|
||||
/* LDRA_EXCLUDE 170 S <justification start> FALSE POSITIVE -- needed for ncurses <justification end> */
|
||||
/* LDRA_EXCLUDE 496 S <justification start> FALSE POSITIVE -- needed for ncurses <justification end> */
|
||||
/* LDRA_EXCLUDE 496 S <justification start> FALSE POSITIVE -- needed for ncurses <justification end> */
|
||||
/* LDRA_EXCLUDE 41 D <justification start> FALSE POSITIVE -- needed for ncurses <justification end> */
|
||||
attroff(COLOR_PAIR(2));
|
||||
break;
|
||||
case ' ':
|
||||
/* LDRA_EXCLUDE 170 S <justification start> FALSE POSITIVE -- needed for ncurses <justification end> */
|
||||
/* LDRA_EXCLUDE 496 S <justification start> FALSE POSITIVE -- needed for ncurses <justification end> */
|
||||
wmove(W, c1,c2);
|
||||
/* LDRA_EXCLUDE 170 S <justification start> FALSE POSITIVE -- needed for ncurses <justification end> */
|
||||
/* LDRA_EXCLUDE 496 S <justification start> FALSE POSITIVE -- needed for ncurses <justification end> */
|
||||
waddch(W,' ');
|
||||
break;
|
||||
case 'P':
|
||||
/* LDRA_EXCLUDE 170 S <justification start> FALSE POSITIVE -- needed for ncurses <justification end> */
|
||||
/* LDRA_EXCLUDE 496 S <justification start> FALSE POSITIVE -- needed for ncurses <justification end> */
|
||||
wmove(W, c1,c2);
|
||||
/* LDRA_EXCLUDE 170 S <justification start> FALSE POSITIVE -- needed for ncurses <justification end> */
|
||||
/* LDRA_EXCLUDE 496 S <justification start> FALSE POSITIVE -- needed for ncurses <justification end> */
|
||||
waddch(W,'P');
|
||||
prow=c1;
|
||||
pcol=c2;
|
||||
break;
|
||||
case 'Z':
|
||||
/* LDRA_EXCLUDE 170 S <justification start> FALSE POSITIVE -- needed for ncurses <justification end> */
|
||||
/* LDRA_EXCLUDE 170 S <justification start> FALSE POSITIVE -- needed for ncurses <justification end> */
|
||||
/* LDRA_EXCLUDE 496 S <justification start> FALSE POSITIVE -- needed for ncurses <justification end> */
|
||||
/* LDRA_EXCLUDE 496 S <justification start> FALSE POSITIVE -- needed for ncurses <justification end> */
|
||||
attron(COLOR_PAIR(4));
|
||||
/* LDRA_EXCLUDE 170 S <justification start> FALSE POSITIVE -- needed for ncurses <justification end> */
|
||||
/* LDRA_EXCLUDE 496 S <justification start> FALSE POSITIVE -- needed for ncurses <justification end> */
|
||||
wmove(W, c1,c2);
|
||||
/* LDRA_EXCLUDE 170 S <justification start> FALSE POSITIVE -- needed for ncurses <justification end> */
|
||||
/* LDRA_EXCLUDE 496 S <justification start> FALSE POSITIVE -- needed for ncurses <justification end> */
|
||||
waddch(W,'Z');
|
||||
/* LDRA_EXCLUDE 170 S <justification start> FALSE POSITIVE -- needed for ncurses <justification end> */
|
||||
/* LDRA_EXCLUDE 170 S <justification start> FALSE POSITIVE -- needed for ncurses <justification end> */
|
||||
/* LDRA_EXCLUDE 496 S <justification start> FALSE POSITIVE -- needed for ncurses <justification end> */
|
||||
/* LDRA_EXCLUDE 496 S <justification start> FALSE POSITIVE -- needed for ncurses <justification end> */
|
||||
attroff(COLOR_PAIR(4));
|
||||
break;
|
||||
case 'T':
|
||||
/* LDRA_EXCLUDE 170 S <justification start> FALSE POSITIVE -- needed for ncurses <justification end> */
|
||||
/* LDRA_EXCLUDE 170 S <justification start> FALSE POSITIVE -- needed for ncurses <justification end> */
|
||||
/* LDRA_EXCLUDE 496 S <justification start> FALSE POSITIVE -- needed for ncurses <justification end> */
|
||||
/* LDRA_EXCLUDE 496 S <justification start> FALSE POSITIVE -- needed for ncurses <justification end> */
|
||||
attron(COLOR_PAIR(5));
|
||||
/* LDRA_EXCLUDE 170 S <justification start> FALSE POSITIVE -- needed for ncurses <justification end> */
|
||||
/* LDRA_EXCLUDE 496 S <justification start> FALSE POSITIVE -- needed for ncurses <justification end> */
|
||||
wmove(W, c1,c2);
|
||||
/* LDRA_EXCLUDE 170 S <justification start> FALSE POSITIVE -- needed for ncurses <justification end> */
|
||||
/* LDRA_EXCLUDE 496 S <justification start> FALSE POSITIVE -- needed for ncurses <justification end> */
|
||||
waddch(W,'T');
|
||||
/* LDRA_EXCLUDE 170 S <justification start> FALSE POSITIVE -- needed for ncurses <justification end> */
|
||||
/* LDRA_EXCLUDE 170 S <justification start> FALSE POSITIVE -- needed for ncurses <justification end> */
|
||||
/* LDRA_EXCLUDE 496 S <justification start> FALSE POSITIVE -- needed for ncurses <justification end> */
|
||||
/* LDRA_EXCLUDE 496 S <justification start> FALSE POSITIVE -- needed for ncurses <justification end> */
|
||||
attroff(COLOR_PAIR(5));
|
||||
break;
|
||||
|
||||
default:
|
||||
break;
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
deltaRow = 0;
|
||||
deltaCol = 0;
|
||||
|
||||
|
||||
|
||||
do{
|
||||
|
||||
/* LDRA_EXCLUDE 170 S <justification start> FALSE POSITIVE -- needed for ncurses <justification end> */
|
||||
/* LDRA_EXCLUDE 496 S <justification start> FALSE POSITIVE -- needed for ncurses <justification end> */
|
||||
int kb = wgetch(W); /* Grab keypress */
|
||||
|
||||
/* Support player movement via WASD, IJKL, and arrow keys */
|
||||
if (kb == (int)'a' || kb == (int)'j' || kb == KEY_LEFT)
|
||||
{
|
||||
/* Move Left */
|
||||
deltaRow =0;
|
||||
deltaCol =-1;
|
||||
}
|
||||
else if (kb == (int)'d' || kb == (int)'l' || kb == KEY_RIGHT)
|
||||
{
|
||||
/* Move Right */
|
||||
deltaRow =0;
|
||||
deltaCol =1;
|
||||
}
|
||||
else if (kb == (int)'w' || kb == (int)'i' || kb == KEY_UP)
|
||||
{
|
||||
/* Move Up */
|
||||
deltaRow =-1;
|
||||
deltaCol =0;
|
||||
}
|
||||
else if (kb == (int)'s' || kb == (int)'k' || kb == KEY_DOWN)
|
||||
{
|
||||
/* Move Down */
|
||||
deltaRow =1;
|
||||
deltaCol =0;
|
||||
}
|
||||
else if (kb == (int)'q')
|
||||
{
|
||||
/* q to exit player movement loop */
|
||||
break;
|
||||
};
|
||||
|
||||
/* LDRA_EXCLUDE 170 S <justification start> FALSE POSITIVE -- needed for ncurses <justification end> */
|
||||
/* LDRA_EXCLUDE 170 S <justification start> FALSE POSITIVE -- needed for ncurses <justification end> */
|
||||
/* LDRA_EXCLUDE 496 S <justification start> FALSE POSITIVE -- needed for ncurses <justification end> */
|
||||
/* LDRA_EXCLUDE 496 S <justification start> FALSE POSITIVE -- needed for ncurses <justification end> */
|
||||
attron(COLOR_PAIR(1));
|
||||
/* LDRA_EXCLUDE 170 S <justification start> FALSE POSITIVE -- needed for ncurses <justification end> */
|
||||
/* LDRA_EXCLUDE 496 S <justification start> FALSE POSITIVE -- needed for ncurses <justification end> */
|
||||
wmove(W,prow,pcol);
|
||||
/* LDRA_EXCLUDE 170 S <justification start> FALSE POSITIVE -- needed for ncurses <justification end> */
|
||||
/* LDRA_EXCLUDE 496 S <justification start> FALSE POSITIVE -- needed for ncurses <justification end> */
|
||||
waddch(W, 'P');
|
||||
/* LDRA_EXCLUDE 170 S <justification start> FALSE POSITIVE -- needed for ncurses <justification end> */
|
||||
/* LDRA_EXCLUDE 170 S <justification start> FALSE POSITIVE -- needed for ncurses <justification end> */
|
||||
/* LDRA_EXCLUDE 496 S <justification start> FALSE POSITIVE -- needed for ncurses <justification end> */
|
||||
/* LDRA_EXCLUDE 496 S <justification start> FALSE POSITIVE -- needed for ncurses <justification end> */
|
||||
attroff(COLOR_PAIR(1));
|
||||
/* check current player position against maze bounds,
|
||||
if over the bounds, don't move */
|
||||
if(((prow + (unsigned int)deltaRow) > row) || ((pcol + (unsigned int)deltaCol) > col)){
|
||||
deltaRow = 0;
|
||||
deltaCol = 0;
|
||||
}
|
||||
/* check future player position to see if wall;
|
||||
if it is, stay in the same spot
|
||||
else move to new postion */
|
||||
if(maze[((int)prow + deltaRow)][((int)pcol + deltaCol)] == '#'){
|
||||
prow = prow;
|
||||
pcol = pcol;
|
||||
}
|
||||
else{
|
||||
prow = prow + (unsigned int)deltaRow;
|
||||
pcol = pcol + (unsigned int)deltaCol;
|
||||
}
|
||||
/* check curremt position for treasure,
|
||||
make treasure position a space,
|
||||
and increment score */
|
||||
if(maze[prow][pcol] == 'T')
|
||||
{
|
||||
maze[prow][pcol] = ' ';
|
||||
score++;
|
||||
}
|
||||
/* check curremt position for zombie,
|
||||
end game */
|
||||
if(maze[prow][pcol] == 'Z')
|
||||
{
|
||||
break;
|
||||
}
|
||||
|
||||
/* LDRA_EXCLUDE 170 S <justification start> FALSE POSITIVE -- needed for ncurses <justification end> */
|
||||
/* LDRA_EXCLUDE 170 S <justification start> FALSE POSITIVE -- needed for ncurses <justification end> */
|
||||
/* LDRA_EXCLUDE 496 S <justification start> FALSE POSITIVE -- needed for ncurses <justification end> */
|
||||
/* LDRA_EXCLUDE 496 S <justification start> FALSE POSITIVE -- needed for ncurses <justification end> */
|
||||
attron(COLOR_PAIR(3));
|
||||
/* LDRA_EXCLUDE 170 S <justification start> FALSE POSITIVE -- needed for ncurses <justification end> */
|
||||
/* LDRA_EXCLUDE 496 S <justification start> FALSE POSITIVE -- needed for ncurses <justification end> */
|
||||
wmove(W,prow,pcol);
|
||||
/* LDRA_EXCLUDE 170 S <justification start> FALSE POSITIVE -- needed for ncurses <justification end> */
|
||||
/* LDRA_EXCLUDE 496 S <justification start> FALSE POSITIVE -- needed for ncurses <justification end> */
|
||||
waddch(W, 'P');
|
||||
/* LDRA_EXCLUDE 170 S <justification start> FALSE POSITIVE -- needed for ncurses <justification end> */
|
||||
/* LDRA_EXCLUDE 170 S <justification start> FALSE POSITIVE -- needed for ncurses <justification end> */
|
||||
/* LDRA_EXCLUDE 496 S <justification start> FALSE POSITIVE -- needed for ncurses <justification end> */
|
||||
/* LDRA_EXCLUDE 496 S <justification start> FALSE POSITIVE -- needed for ncurses <justification end> */
|
||||
attroff(COLOR_PAIR(3));
|
||||
|
||||
deltaRow =0;
|
||||
deltaCol =0;
|
||||
|
||||
/* LDRA_EXCLUDE 170 S <justification start> FALSE POSITIVE -- needed for ncurses <justification end> */
|
||||
/* LDRA_EXCLUDE 496 S <justification start> FALSE POSITIVE -- needed for ncurses <justification end> */
|
||||
wrefresh(W); /* Refresh ncurses window */
|
||||
|
||||
} while (true); /* Repeat until user selects q to quit */
|
||||
|
||||
/* free the memory */
|
||||
for(y = (unsigned)0; y < row; y++)
|
||||
{
|
||||
/* LDRA_EXCLUDE 44 S <justification start> FALSE POSITIVE -- Need function to free allocated memory, cannot create one ourselves bc we are not wizards <justification end> */
|
||||
free(maze[y]);
|
||||
maze[y] = NULL;
|
||||
}
|
||||
if(maze != NULL){
|
||||
/* LDRA_EXCLUDE 44 S <justification start> FALSE POSITIVE -- Need function to free allocated memory, cannot create one ourselves bc we are not wizards <justification end> */
|
||||
free(maze);
|
||||
}
|
||||
/*printw("\n");*/
|
||||
/* LDRA_EXCLUDE 170 S <justification start> FALSE POSITIVE -- needed for ncurses <justification end> */
|
||||
/* LDRA_EXCLUDE 496 S <justification start> FALSE POSITIVE -- needed for ncurses <justification end> */
|
||||
/* LDRA_EXCLUDE 41 D <justification start> FALSE POSITIVE -- needed for ncurses <justification end> */
|
||||
endwin();
|
||||
/* LDRA_EXCLUDE 44 S <justification start> FALSE POSITIVE -- Need function to print score for project, so even though not great have to use bad one <justification end> */
|
||||
printf("Score = %u\n", score);
|
||||
return 0;
|
||||
/* LDRA_EXCLUDE 50 D <justification start> FALSE POSITIVE -- Y was statically allocated no need to free it <justification end> */
|
||||
/* LDRA_EXCLUDE 50 D <justification start> FALSE POSITIVE -- Maze has been freed already valgrind shows no leaks error must have ocurred in analysis <justification end> */
|
||||
}
|
||||
|
@ -0,0 +1,8 @@
|
||||
CC = gcc
|
||||
CFLAGS = -g -Wall --pedantic -ansi --coverage
|
||||
|
||||
game: game.c
|
||||
$(CC) $(CFLAGS) game.c -o game -l ncurses
|
||||
|
||||
clean:
|
||||
rm game *.gcda *.gcno *.gcov
|
@ -0,0 +1,126 @@
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <stdint.h>
|
||||
#include <string.h>
|
||||
|
||||
void readMaze(char *arr);
|
||||
void readPlayer(char *arr);
|
||||
void readZombie(char *arr);
|
||||
void readTreasure(char *arr);
|
||||
|
||||
int main(int argc, char *argv[])
|
||||
{
|
||||
printf("Hello, World!\n");
|
||||
FILE* mazeFile; /* Maze Input File */
|
||||
char c; /* Holds character for line input from file */
|
||||
int row; /* Holds number of rows for input from file */
|
||||
int col; /* Holds number of cols for input from file */
|
||||
char shold[2000]; /* Holds lines for input from file */
|
||||
|
||||
if (argc != 2)
|
||||
{
|
||||
printf("An error has ocurred no input file was given.\n");
|
||||
return 1;
|
||||
}
|
||||
mazeFile = fopen(argv[1], "r");
|
||||
if (mazeFile == NULL)
|
||||
{
|
||||
printf("Error in opening the file.\n");
|
||||
return 1;
|
||||
}
|
||||
fgets(shold, 2, mazeFile);
|
||||
while ( !feof(mazeFile) )
|
||||
{
|
||||
|
||||
switch(shold[0])
|
||||
{
|
||||
case 'M':
|
||||
printf ("Maze encountered\n");
|
||||
fscanf(mazeFile, "%d %d", &row, &col);
|
||||
printf("%d\n", row);
|
||||
// possible dynamic allocation of 2D array
|
||||
char* maze = malloc((row * col) * sizeof(char));
|
||||
readMaze(maze);
|
||||
break;
|
||||
case 'P':
|
||||
//case for player
|
||||
printf ("Player\n");
|
||||
fscanf(mazeFile, "%d %d", &row, &col);
|
||||
printf("%d\n", row);
|
||||
// possible dynamic allocation of 2D array
|
||||
char* player = malloc((row * col) * sizeof(char));
|
||||
readPlayer(player);
|
||||
break;
|
||||
|
||||
case 'Z':
|
||||
//case for zombie
|
||||
printf ("Zombie\n");
|
||||
fscanf(mazeFile, "%d %d", &row, &col);
|
||||
printf("%d\n", row);
|
||||
// possible dynamic allocation of 2D array
|
||||
char* zombie = malloc((row * col) * sizeof(char));
|
||||
readZombie(zombie);
|
||||
break;
|
||||
|
||||
case 'T':
|
||||
//case for treasure
|
||||
printf ("Treasure\n");
|
||||
fscanf(mazeFile, "%d %d", &row, &col);
|
||||
printf("%d\n", row);
|
||||
// possible dynamic allocation of 2D array
|
||||
char* treasure = malloc((row * col) * sizeof(char));
|
||||
readTreasure(treasure);
|
||||
break;
|
||||
default:
|
||||
//printf ("Basic error\n");
|
||||
break;
|
||||
};
|
||||
|
||||
for(int i=0;i<row;i++)
|
||||
{
|
||||
for(int j=0;j<col;j++)
|
||||
{
|
||||
if(fscanf == #) //if file has #
|
||||
{
|
||||
maze[i][j] = #;
|
||||
}
|
||||
else
|
||||
{
|
||||
maze[i][j] = " ";
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fgets(shold, 2, mazeFile);
|
||||
if (feof(mazeFile))
|
||||
{
|
||||
break;
|
||||
}
|
||||
}
|
||||
fclose(mazeFile);
|
||||
//printf ("So this line won't print without an argument.\n");
|
||||
|
||||
//free the memory
|
||||
free(maze);
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
void readMaze(char *arr)
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
void readPlayer(char *arr)
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
void readZombie(char *arr)
|
||||
{
|
||||
|
||||
}
|
||||
void readTreasure(char *arr)
|
||||
{
|
||||
|
||||
}
|
@ -0,0 +1,16 @@
|
||||
M 12 40
|
||||
########################################
|
||||
# # # #
|
||||
# # # #
|
||||
# # # #
|
||||
# # ###### ############ ### #
|
||||
# # # # # #
|
||||
# # # # # #
|
||||
# # ###### #### ########## #
|
||||
# # # #
|
||||
# # # #
|
||||
# # # #
|
||||
########################################
|
||||
T 8 15
|
||||
Z 8 10
|
||||
P 2 10
|
@ -0,0 +1,178 @@
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <stdint.h>
|
||||
#include <string.h>
|
||||
#include <unistd.h>
|
||||
#include <ncurses.h>
|
||||
|
||||
|
||||
|
||||
|
||||
int main(int argc, char *argv[])
|
||||
{
|
||||
FILE* mazeFile; /* Maze Input File */
|
||||
unsigned int row = 0; /* Holds number of rows for input from file */
|
||||
unsigned int col = 0; /* Holds number of cols for input from file */
|
||||
char shold[2000]; /* Holds lines for input from file */
|
||||
unsigned int l = 0;
|
||||
unsigned int k = 0;
|
||||
char **maze; /* array for maze */
|
||||
unsigned int c1 = 0;
|
||||
unsigned int c2 = 0;
|
||||
unsigned int i = 0;
|
||||
unsigned int j = 0;
|
||||
unsigned int x = 0;
|
||||
unsigned int y = 0;
|
||||
WINDOW* W = 0; /* Curses Window handle */
|
||||
int rowLimit,colLimit; /* Num of rows and columns in terminal window */
|
||||
/* Initialize ncurses -- you will want to use the following settings */
|
||||
|
||||
|
||||
/* check to see if argument */
|
||||
if (argc != 2)
|
||||
{
|
||||
/*printw("An error has ocurred no input file was given.\n");*/
|
||||
return 1;
|
||||
}
|
||||
mazeFile = fopen(argv[1], "r");
|
||||
if (mazeFile == NULL)
|
||||
{
|
||||
/*printw("Error in opening the file.\n");*/
|
||||
return 1;
|
||||
}
|
||||
fgets(shold, 2, mazeFile);
|
||||
while ( feof(mazeFile) == 0 )
|
||||
{
|
||||
|
||||
switch(shold[0])
|
||||
{
|
||||
case 'M':
|
||||
/* read array values, dynamically allocate array */
|
||||
fscanf(mazeFile, "%3u %3u", &row, &col);
|
||||
maze = (char**)malloc(row * 8);
|
||||
for(i = (unsigned) 0; i < row; i++)
|
||||
{
|
||||
if (maze==NULL)
|
||||
{
|
||||
break;
|
||||
}
|
||||
maze[i] = malloc(col * 8);
|
||||
}
|
||||
for (x = (unsigned) 0; x < row; x++)
|
||||
{
|
||||
for (j = (unsigned) 0; j < col; j++)
|
||||
{
|
||||
fscanf(mazeFile, "%c", &maze[x][j]);
|
||||
if (maze[x][j] == '\n')
|
||||
{
|
||||
fscanf(mazeFile, "%c", &maze[x][j]);
|
||||
}
|
||||
}
|
||||
}
|
||||
break;
|
||||
case 'P':
|
||||
fscanf(mazeFile, "%3u %3u", &k, &l);
|
||||
/* check maze bounds */
|
||||
if(k>= row || l>= col){
|
||||
break;
|
||||
}
|
||||
maze[k][l] = 'P';
|
||||
break;
|
||||
|
||||
case 'Z':
|
||||
fscanf(mazeFile, "%3u %3u", &k, &l);
|
||||
/* check maze bounds */
|
||||
if(k>= row || l>= col){
|
||||
break;
|
||||
}
|
||||
maze[k][l] = 'Z';
|
||||
break;
|
||||
|
||||
case 'T':
|
||||
fscanf(mazeFile, "%3u %3u", &k, &l);
|
||||
/* check maze bounds */
|
||||
if(k>= row || l>= col){
|
||||
break;
|
||||
}
|
||||
maze[k][l] = 'T';
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
};
|
||||
fgets(shold, 2, mazeFile);
|
||||
if (feof(mazeFile) != 0)
|
||||
{
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
fclose(mazeFile);
|
||||
W = initscr(); /* Determine terminal type and initialize curses data structures */
|
||||
start_color(); /* Enable use of color with ncurses */
|
||||
cbreak(); /* Disable line buffering and make char inputs immediately accessible to program */
|
||||
noecho(); /* Disable echo printing of chars type by user */
|
||||
nodelay(W, true); /* Make getch( ) a non-blocking call */
|
||||
keypad(W, true); /* Enable keypad and arrow keys */
|
||||
curs_set(0); /* Set cursor to be invisible - 0 */
|
||||
getmaxyx(W, rowLimit, colLimit); /* Query terminal dimensions */
|
||||
|
||||
for (c1 =(unsigned)0; c1 < row; c1++)
|
||||
{
|
||||
/*printw ("\n");*/
|
||||
for (c2=(unsigned)0; c2 < col; c2++)
|
||||
{
|
||||
if (maze==NULL)
|
||||
{
|
||||
break;
|
||||
}
|
||||
switch(maze[c1][c2])
|
||||
{
|
||||
case '#':
|
||||
/* read array values, dynamically allocate array*/
|
||||
wmove(W, c1,c2);
|
||||
waddch(W,'#');
|
||||
break;
|
||||
case ' ':
|
||||
wmove(W, c1,c2);
|
||||
waddch(W,' ');
|
||||
break;
|
||||
case 'P':
|
||||
wmove(W, c1,c2);
|
||||
waddch(W,'P');
|
||||
break;
|
||||
case 'Z':
|
||||
wmove(W, c1,c2);
|
||||
waddch(W,'Z');
|
||||
break;
|
||||
case 'T':
|
||||
wmove(W, c1,c2);
|
||||
waddch(W,'T');
|
||||
break;
|
||||
|
||||
default:
|
||||
break;
|
||||
};
|
||||
/*printw("%c", maze[c1][c2]);*/
|
||||
}
|
||||
}
|
||||
do{
|
||||
|
||||
wrefresh(W); /* Refresh ncurses window */
|
||||
|
||||
} while (true); /* Repeat until user selects q to quit */
|
||||
|
||||
|
||||
/* free the memory */
|
||||
for(y = (unsigned)0; y < row; y++)
|
||||
{
|
||||
free(maze[y]);
|
||||
maze[y] = NULL;
|
||||
}
|
||||
if(maze != NULL){
|
||||
free(maze);
|
||||
}
|
||||
/*printw("\n");*/
|
||||
endwin();
|
||||
return 0;
|
||||
}
|
@ -0,0 +1,270 @@
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <stdint.h>
|
||||
#include <string.h>
|
||||
#include <unistd.h>
|
||||
#include <ncurses.h>
|
||||
|
||||
|
||||
/*nothing*/
|
||||
|
||||
int main(int argc, char *argv[])
|
||||
{
|
||||
FILE* mazeFile; /* Maze Input File */
|
||||
unsigned int row = 0; /* Holds number of rows for input from file */
|
||||
unsigned int col = 0; /* Holds number of cols for input from file */
|
||||
unsigned int pcol = 0;
|
||||
unsigned int prow = 0;
|
||||
char shold[2000]; /* Holds lines for input from file */
|
||||
unsigned int l = 0;
|
||||
unsigned int k = 0;
|
||||
char **maze; /* array for maze */
|
||||
unsigned int c1 = 0;
|
||||
unsigned int c2 = 0;
|
||||
unsigned int i = 0;
|
||||
unsigned int j = 0;
|
||||
unsigned int x = 0;
|
||||
unsigned int y = 0;
|
||||
int deltaRow, deltaCol; /* Change in row-column position based on key input */
|
||||
|
||||
|
||||
WINDOW* W = 0; /* Curses Window handle */
|
||||
int rowLimit,colLimit; /* Num of rows and columns in terminal window */
|
||||
/* Initialize ncurses -- you will want to use the following settings */
|
||||
|
||||
|
||||
|
||||
/* check to see if argument */
|
||||
if (argc != 2)
|
||||
{
|
||||
/*printw("An error has ocurred no input file was given.\n");*/
|
||||
return 1;
|
||||
}
|
||||
mazeFile = fopen(argv[1], "r");
|
||||
if (mazeFile == NULL)
|
||||
{
|
||||
/*printw("Error in opening the file.\n");*/
|
||||
return 1;
|
||||
}
|
||||
fgets(shold, 2, mazeFile);
|
||||
while ( feof(mazeFile) == 0 )
|
||||
{
|
||||
|
||||
|
||||
switch(shold[0])
|
||||
{
|
||||
case 'M':
|
||||
/* read array values, dynamically allocate array */
|
||||
fscanf(mazeFile, "%3u %3u", &row, &col);
|
||||
maze = (char**)malloc(row * 8);
|
||||
for(i = (unsigned) 0; i < row; i++)
|
||||
{
|
||||
if (maze==NULL)
|
||||
{
|
||||
break;
|
||||
}
|
||||
maze[i] = malloc(col * 8);
|
||||
}
|
||||
for (x = (unsigned) 0; x < row; x++)
|
||||
{
|
||||
for (j = (unsigned) 0; j < col; j++)
|
||||
{
|
||||
fscanf(mazeFile, "%c", &maze[x][j]);
|
||||
if (maze[x][j] == '\n')
|
||||
{
|
||||
fscanf(mazeFile, "%c", &maze[x][j]);
|
||||
}
|
||||
}
|
||||
}
|
||||
break;
|
||||
case 'P':
|
||||
fscanf(mazeFile, "%3u %3u", &k, &l);
|
||||
/* check maze bounds */
|
||||
if(k>= row || l>= col){
|
||||
break;
|
||||
}
|
||||
maze[k][l] = 'P';
|
||||
break;
|
||||
|
||||
case 'Z':
|
||||
fscanf(mazeFile, "%3u %3u", &k, &l);
|
||||
/* check maze bounds */
|
||||
if(k>= row || l>= col){
|
||||
break;
|
||||
}
|
||||
maze[k][l] = 'Z';
|
||||
break;
|
||||
|
||||
case 'T':
|
||||
fscanf(mazeFile, "%3u %3u", &k, &l);
|
||||
/* check maze bounds */
|
||||
if(k>= row || l>= col){
|
||||
break;
|
||||
}
|
||||
maze[k][l] = 'T';
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
};
|
||||
fgets(shold, 2, mazeFile);
|
||||
if (feof(mazeFile) != 0)
|
||||
{
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
fclose(mazeFile);
|
||||
W = initscr(); /* Determine terminal type and initialize curses data structures */
|
||||
start_color(); /* Enable use of color with ncurses */
|
||||
cbreak(); /* Disable line buffering and make char inputs immediately accessible to program */
|
||||
noecho(); /* Disable echo printing of chars type by user */
|
||||
nodelay(W, true); /* Make getch( ) a non-blocking call */
|
||||
keypad(W, true); /* Enable keypad and arrow keys */
|
||||
curs_set(0); /* Set cursor to be invisible - 0 */
|
||||
getmaxyx(W, rowLimit, colLimit); /* Query terminal dimensions */
|
||||
|
||||
/* Define color pallete foreground-background color pairs */
|
||||
/* PairID#, Foreground Color, Background Color */
|
||||
/* Foreground color == Background color ==> solid color block */
|
||||
init_pair(1, COLOR_BLACK, COLOR_BLACK); /* Black text on Black background */
|
||||
init_pair(2, COLOR_GREEN, COLOR_GREEN); /* Red text on Black background */
|
||||
init_pair(3, COLOR_WHITE, COLOR_WHITE); /* White text on Blue background */
|
||||
init_pair(4, COLOR_RED, COLOR_RED); /* Yellow text on Yellow background */
|
||||
init_pair(5, COLOR_YELLOW, COLOR_YELLOW); /* Yellow text on Yellow background */
|
||||
|
||||
|
||||
for (c1 =(unsigned)0; c1 < row; c1++)
|
||||
{
|
||||
/*printw ("\n");*/
|
||||
for (c2=(unsigned)0; c2 < col; c2++)
|
||||
{
|
||||
if (maze==NULL)
|
||||
{
|
||||
break;
|
||||
}
|
||||
switch(maze[c1][c2])
|
||||
{
|
||||
case '#':
|
||||
/* read array values, dynamically allocate array*/
|
||||
attron(COLOR_PAIR(2));
|
||||
wmove(W, c1,c2);
|
||||
waddch(W,'#');
|
||||
attroff(COLOR_PAIR(2));
|
||||
break;
|
||||
case ' ':
|
||||
wmove(W, c1,c2);
|
||||
waddch(W,' ');
|
||||
break;
|
||||
case 'P':
|
||||
wmove(W, c1,c2);
|
||||
waddch(W,'P');
|
||||
prow=c1;
|
||||
pcol=c2;
|
||||
break;
|
||||
case 'Z':
|
||||
attron(COLOR_PAIR(4));
|
||||
wmove(W, c1,c2);
|
||||
waddch(W,'Z');
|
||||
attroff(COLOR_PAIR(4));
|
||||
break;
|
||||
case 'T':
|
||||
attron(COLOR_PAIR(5));
|
||||
wmove(W, c1,c2);
|
||||
waddch(W,'T');
|
||||
attroff(COLOR_PAIR(5));
|
||||
break;
|
||||
|
||||
default:
|
||||
break;
|
||||
};
|
||||
/*printw("%c", maze[c1][c2]);*/
|
||||
}
|
||||
}
|
||||
|
||||
deltaRow =0;
|
||||
deltaCol =0;
|
||||
|
||||
|
||||
|
||||
do{
|
||||
|
||||
int kb = wgetch(W); /* Grab keypress */
|
||||
|
||||
/* Support player movement via WASD, IJKL, and arrow keys */
|
||||
if (kb == 'a' || kb == 'j' || kb == KEY_LEFT)
|
||||
{
|
||||
/* Move Left */
|
||||
deltaRow =0;
|
||||
deltaCol =-1;
|
||||
}
|
||||
else if (kb == 'd' || kb == 'l' || kb == KEY_RIGHT)
|
||||
{
|
||||
/* Move Right */
|
||||
deltaRow =0;
|
||||
deltaCol =1;
|
||||
}
|
||||
else if (kb == 'w' || kb == 'i' || kb == KEY_UP)
|
||||
{
|
||||
/* Move Up */
|
||||
deltaRow =-1;
|
||||
deltaCol =0;
|
||||
}
|
||||
else if (kb == 's' || kb == 'k' || kb == KEY_DOWN)
|
||||
{
|
||||
/* Move Down */
|
||||
deltaRow =1;
|
||||
deltaCol =0;
|
||||
}
|
||||
else if (kb == 'q')
|
||||
{
|
||||
/* q to exit player movement loop */
|
||||
break;
|
||||
};
|
||||
|
||||
|
||||
attron(COLOR_PAIR(1));
|
||||
wmove(W,prow,pcol);
|
||||
waddch(W, 'P');
|
||||
attroff(COLOR_PAIR(1));
|
||||
|
||||
if((prow + deltaRow > row) || (pcol + deltaCol > col)){
|
||||
deltaRow = 0;
|
||||
deltaCol = 0;
|
||||
}
|
||||
if(maze[prow + deltaRow][pcol + deltaCol] == '#'){
|
||||
prow = prow;
|
||||
pcol = pcol;
|
||||
}
|
||||
else{
|
||||
prow = prow + deltaRow;
|
||||
pcol = pcol + deltaCol;
|
||||
}
|
||||
|
||||
attron(COLOR_PAIR(3));
|
||||
wmove(W,prow,pcol);
|
||||
waddch(W, 'P');
|
||||
attroff(COLOR_PAIR(3));
|
||||
|
||||
deltaRow =0;
|
||||
deltaCol =0;
|
||||
|
||||
wrefresh(W); /* Refresh ncurses window */
|
||||
|
||||
} while (true); /* Repeat until user selects q to quit */
|
||||
|
||||
|
||||
/* free the memory */
|
||||
for(y = (unsigned)0; y < row; y++)
|
||||
{
|
||||
free(maze[y]);
|
||||
maze[y] = NULL;
|
||||
}
|
||||
if(maze != NULL){
|
||||
free(maze);
|
||||
}
|
||||
/*printw("\n");*/
|
||||
endwin();
|
||||
return 0;
|
||||
}
|
||||
|
@ -0,0 +1,13 @@
|
||||
#include <stdio.h>
|
||||
|
||||
int main(int argc, char const *argv[])
|
||||
{
|
||||
printf("Hello, World!\n");
|
||||
if (argc != 2)
|
||||
{
|
||||
printf("An error has ocurred no input file was given.\n");
|
||||
return 1;
|
||||
}
|
||||
printf ("So this line won't print without an argument.\n");
|
||||
return 0;
|
||||
}
|
@ -0,0 +1,279 @@
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <stdint.h>
|
||||
#include <string.h>
|
||||
#include <unistd.h>
|
||||
#include <ncurses.h>
|
||||
|
||||
|
||||
/*nothing*/
|
||||
|
||||
int main(int argc, char *argv[])
|
||||
{
|
||||
FILE* mazeFile; /* Maze Input File */
|
||||
unsigned int row = 0; /* Holds number of rows for input from file */
|
||||
unsigned int col = 0; /* Holds number of cols for input from file */
|
||||
unsigned int pcol = 0;
|
||||
unsigned int prow = 0;
|
||||
char shold[2000]; /* Holds lines for input from file */
|
||||
unsigned int l = 0;
|
||||
unsigned int k = 0;
|
||||
char **maze; /* array for maze */
|
||||
unsigned int c1 = 0;
|
||||
unsigned int c2 = 0;
|
||||
unsigned int i = 0;
|
||||
unsigned int j = 0;
|
||||
unsigned int x = 0;
|
||||
unsigned int y = 0;
|
||||
int deltaRow, deltaCol; /* Change in row-column position based on key input */
|
||||
unsigned int score = 0;
|
||||
|
||||
|
||||
WINDOW* W = 0; /* Curses Window handle */
|
||||
int rowLimit,colLimit; /* Num of rows and columns in terminal window */
|
||||
/* Initialize ncurses -- you will want to use the following settings */
|
||||
|
||||
|
||||
|
||||
/* check to see if argument */
|
||||
if (argc != 2)
|
||||
{
|
||||
/*printw("An error has ocurred no input file was given.\n");*/
|
||||
return 1;
|
||||
}
|
||||
mazeFile = fopen(argv[1], "r");
|
||||
if (mazeFile == NULL)
|
||||
{
|
||||
/*printw("Error in opening the file.\n");*/
|
||||
return 1;
|
||||
}
|
||||
fgets(shold, 2, mazeFile);
|
||||
while ( feof(mazeFile) == 0 )
|
||||
{
|
||||
|
||||
|
||||
switch(shold[0])
|
||||
{
|
||||
case 'M':
|
||||
/* read array values, dynamically allocate array */
|
||||
fscanf(mazeFile, "%3u %3u", &row, &col);
|
||||
maze = (char**)malloc(row * 8);
|
||||
for(i = (unsigned) 0; i < row; i++)
|
||||
{
|
||||
if (maze==NULL)
|
||||
{
|
||||
break;
|
||||
}
|
||||
maze[i] = malloc(col * 8);
|
||||
}
|
||||
for (x = (unsigned) 0; x < row; x++)
|
||||
{
|
||||
for (j = (unsigned) 0; j < col; j++)
|
||||
{
|
||||
fscanf(mazeFile, "%c", &maze[x][j]);
|
||||
if (maze[x][j] == '\n')
|
||||
{
|
||||
fscanf(mazeFile, "%c", &maze[x][j]);
|
||||
}
|
||||
}
|
||||
}
|
||||
break;
|
||||
case 'P':
|
||||
fscanf(mazeFile, "%3u %3u", &k, &l);
|
||||
/* check maze bounds */
|
||||
if(k>= row || l>= col){
|
||||
break;
|
||||
}
|
||||
maze[k][l] = 'P';
|
||||
break;
|
||||
|
||||
case 'Z':
|
||||
fscanf(mazeFile, "%3u %3u", &k, &l);
|
||||
/* check maze bounds */
|
||||
if(k>= row || l>= col){
|
||||
break;
|
||||
}
|
||||
maze[k][l] = 'Z';
|
||||
break;
|
||||
|
||||
case 'T':
|
||||
fscanf(mazeFile, "%3u %3u", &k, &l);
|
||||
/* check maze bounds */
|
||||
if(k>= row || l>= col){
|
||||
break;
|
||||
}
|
||||
maze[k][l] = 'T';
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
};
|
||||
fgets(shold, 2, mazeFile);
|
||||
if (feof(mazeFile) != 0)
|
||||
{
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
fclose(mazeFile);
|
||||
W = initscr(); /* Determine terminal type and initialize curses data structures */
|
||||
start_color(); /* Enable use of color with ncurses */
|
||||
cbreak(); /* Disable line buffering and make char inputs immediately accessible to program */
|
||||
noecho(); /* Disable echo printing of chars type by user */
|
||||
nodelay(W, true); /* Make getch( ) a non-blocking call */
|
||||
keypad(W, true); /* Enable keypad and arrow keys */
|
||||
curs_set(0); /* Set cursor to be invisible - 0 */
|
||||
getmaxyx(W, rowLimit, colLimit); /* Query terminal dimensions */
|
||||
|
||||
/* Define color pallete foreground-background color pairs */
|
||||
/* PairID#, Foreground Color, Background Color */
|
||||
/* Foreground color == Background color ==> solid color block */
|
||||
init_pair(1, COLOR_BLACK, COLOR_BLACK); /* Black text on Black background */
|
||||
init_pair(2, COLOR_GREEN, COLOR_GREEN); /* Red text on Black background */
|
||||
init_pair(3, COLOR_WHITE, COLOR_WHITE); /* White text on Blue background */
|
||||
init_pair(4, COLOR_RED, COLOR_RED); /* Yellow text on Yellow background */
|
||||
init_pair(5, COLOR_YELLOW, COLOR_YELLOW); /* Yellow text on Yellow background */
|
||||
|
||||
|
||||
for (c1 =(unsigned)0; c1 < row; c1++)
|
||||
{
|
||||
/*printw ("\n");*/
|
||||
for (c2=(unsigned)0; c2 < col; c2++)
|
||||
{
|
||||
if (maze==NULL)
|
||||
{
|
||||
break;
|
||||
}
|
||||
switch(maze[c1][c2])
|
||||
{
|
||||
case '#':
|
||||
/* read array values, dynamically allocate array*/
|
||||
attron(COLOR_PAIR(2));
|
||||
wmove(W, c1,c2);
|
||||
waddch(W,'#');
|
||||
attroff(COLOR_PAIR(2));
|
||||
break;
|
||||
case ' ':
|
||||
wmove(W, c1,c2);
|
||||
waddch(W,' ');
|
||||
break;
|
||||
case 'P':
|
||||
wmove(W, c1,c2);
|
||||
waddch(W,'P');
|
||||
prow=c1;
|
||||
pcol=c2;
|
||||
break;
|
||||
case 'Z':
|
||||
attron(COLOR_PAIR(4));
|
||||
wmove(W, c1,c2);
|
||||
waddch(W,'Z');
|
||||
attroff(COLOR_PAIR(4));
|
||||
break;
|
||||
case 'T':
|
||||
attron(COLOR_PAIR(5));
|
||||
wmove(W, c1,c2);
|
||||
waddch(W,'T');
|
||||
attroff(COLOR_PAIR(5));
|
||||
break;
|
||||
|
||||
default:
|
||||
break;
|
||||
};
|
||||
/*printw("%c", maze[c1][c2]);*/
|
||||
}
|
||||
}
|
||||
|
||||
deltaRow =0;
|
||||
deltaCol =0;
|
||||
|
||||
|
||||
|
||||
do{
|
||||
|
||||
int kb = wgetch(W); /* Grab keypress */
|
||||
|
||||
/* Support player movement via WASD, IJKL, and arrow keys */
|
||||
if (kb == 'a' || kb == 'j' || kb == KEY_LEFT)
|
||||
{
|
||||
/* Move Left */
|
||||
deltaRow =0;
|
||||
deltaCol =-1;
|
||||
}
|
||||
else if (kb == 'd' || kb == 'l' || kb == KEY_RIGHT)
|
||||
{
|
||||
/* Move Right */
|
||||
deltaRow =0;
|
||||
deltaCol =1;
|
||||
}
|
||||
else if (kb == 'w' || kb == 'i' || kb == KEY_UP)
|
||||
{
|
||||
/* Move Up */
|
||||
deltaRow =-1;
|
||||
deltaCol =0;
|
||||
}
|
||||
else if (kb == 's' || kb == 'k' || kb == KEY_DOWN)
|
||||
{
|
||||
/* Move Down */
|
||||
deltaRow =1;
|
||||
deltaCol =0;
|
||||
}
|
||||
else if (kb == 'q')
|
||||
{
|
||||
/* q to exit player movement loop */
|
||||
break;
|
||||
};
|
||||
|
||||
attron(COLOR_PAIR(1));
|
||||
wmove(W,prow,pcol);
|
||||
waddch(W, 'P');
|
||||
attroff(COLOR_PAIR(1));
|
||||
|
||||
if((prow + deltaRow > row) || (pcol + deltaCol > col)){
|
||||
deltaRow = 0;
|
||||
deltaCol = 0;
|
||||
}
|
||||
if(maze[prow + deltaRow][pcol + deltaCol] == '#'){
|
||||
prow = prow;
|
||||
pcol = pcol;
|
||||
}
|
||||
else{
|
||||
prow = prow + deltaRow;
|
||||
pcol = pcol + deltaCol;
|
||||
}
|
||||
if(maze[prow][pcol] == 'T')
|
||||
{
|
||||
maze[prow][pcol] = ' ';
|
||||
score++;
|
||||
}
|
||||
if(maze[prow][pcol] == 'Z')
|
||||
{
|
||||
break;
|
||||
}
|
||||
|
||||
attron(COLOR_PAIR(3));
|
||||
wmove(W,prow,pcol);
|
||||
waddch(W, 'P');
|
||||
attroff(COLOR_PAIR(3));
|
||||
|
||||
deltaRow =0;
|
||||
deltaCol =0;
|
||||
|
||||
wrefresh(W); /* Refresh ncurses window */
|
||||
|
||||
} while (true); /* Repeat until user selects q to quit */
|
||||
|
||||
/* free the memory */
|
||||
for(y = (unsigned)0; y < row; y++)
|
||||
{
|
||||
free(maze[y]);
|
||||
maze[y] = NULL;
|
||||
}
|
||||
if(maze != NULL){
|
||||
free(maze);
|
||||
}
|
||||
/*printw("\n");*/
|
||||
endwin();
|
||||
printf("Score = %u\n", score);
|
||||
return 0;
|
||||
}
|
||||
|
@ -0,0 +1,14 @@
|
||||
M 12 40
|
||||
########################################
|
||||
# # # #
|
||||
# # # #
|
||||
# # # #
|
||||
# # ###### ############ ### #
|
||||
# # # # # #
|
||||
# # # # # #
|
||||
# # ###### #### ########## #
|
||||
# # # #
|
||||
# # # #
|
||||
# # # #
|
||||
########################################
|
||||
P 2 10
|
@ -0,0 +1,334 @@
|
||||
#include <ncurses.h>
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <stdint.h>
|
||||
#include <string.h>
|
||||
#include <unistd.h>
|
||||
|
||||
extern WINDOW* initscr(void);
|
||||
extern int start_color(void);
|
||||
|
||||
/* LDRA_EXCLUDE RULE VIOLATION <justification start> FALSE POSITIVE -- why <justification end> */
|
||||
|
||||
int main(int argc, char *argv[])
|
||||
{
|
||||
FILE* mazeFile; /* Maze Input File */
|
||||
unsigned int row = 0; /* Holds number of rows for input from file */
|
||||
unsigned int col = 0; /* Holds number of cols for input from file */
|
||||
unsigned int pcol = 0;
|
||||
unsigned int prow = 0;
|
||||
char shold[2000]; /* Holds lines for input from file */
|
||||
unsigned int l = 0;
|
||||
unsigned int k = 0;
|
||||
char **maze; /* array for maze */
|
||||
unsigned int c1 = 0;
|
||||
unsigned int c2 = 0;
|
||||
unsigned int i = 0;
|
||||
unsigned int j = 0;
|
||||
unsigned int x = 0;
|
||||
unsigned int y = 0;
|
||||
int deltaRow, deltaCol; /* Change in row-column position based on key input */
|
||||
unsigned int score = 0;
|
||||
|
||||
|
||||
WINDOW* W = 0; /* Curses Window handle */
|
||||
unsigned int rowLimit,colLimit; /* Num of rows and columns in terminal window */
|
||||
/* Initialize ncurses -- you will want to use the following settings */
|
||||
|
||||
|
||||
|
||||
/* check to see if argument */
|
||||
if (argc != 2)
|
||||
{
|
||||
/*printw("An error has ocurred no input file was given.\n");*/
|
||||
return 1;
|
||||
}
|
||||
mazeFile = fopen(argv[1], "r");
|
||||
if (mazeFile == NULL)
|
||||
{
|
||||
/*printw("Error in opening the file.\n");*/
|
||||
return 1;
|
||||
}
|
||||
fgets(shold, 2, mazeFile);
|
||||
while ( feof(mazeFile) == 0 )
|
||||
{
|
||||
|
||||
|
||||
switch(shold[0])
|
||||
{
|
||||
case 'M':
|
||||
/* read array values, dynamically allocate array */
|
||||
fscanf(mazeFile, "%3u %3u", &row, &col);
|
||||
maze = (char**)malloc(row * 8); /* */
|
||||
for(i = (unsigned) 0; i < row; i++)
|
||||
{
|
||||
if (maze==NULL)
|
||||
{
|
||||
break;
|
||||
}
|
||||
maze[i] = malloc(col * 8);
|
||||
}
|
||||
for (x = (unsigned) 0; x < row; x++)
|
||||
{
|
||||
for (j = (unsigned) 0; j < col; j++)
|
||||
{
|
||||
fscanf(mazeFile, "%c", &maze[x][j]);
|
||||
if (maze[x][j] == '\n')
|
||||
{
|
||||
fscanf(mazeFile, "%c", &maze[x][j]);
|
||||
}
|
||||
}
|
||||
}
|
||||
break;
|
||||
case 'P':
|
||||
fscanf(mazeFile, "%3u %3u", &k, &l);
|
||||
/* check maze bounds */
|
||||
if(k>= row || l>= col){
|
||||
break;
|
||||
}
|
||||
maze[k][l] = 'P';
|
||||
break;
|
||||
|
||||
case 'Z':
|
||||
fscanf(mazeFile, "%3u %3u", &k, &l);
|
||||
/* check maze bounds */
|
||||
if(k>= row || l>= col){
|
||||
break;
|
||||
}
|
||||
maze[k][l] = 'Z';
|
||||
break;
|
||||
|
||||
case 'T':
|
||||
fscanf(mazeFile, "%3u %3u", &k, &l);
|
||||
/* check maze bounds */
|
||||
if(k>= row || l>= col){
|
||||
break;
|
||||
}
|
||||
maze[k][l] = 'T';
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
};
|
||||
fgets(shold, 2, mazeFile);
|
||||
if (feof(mazeFile) != 0)
|
||||
{
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
rowLimit = row;
|
||||
colLimit = col;
|
||||
fclose(mazeFile);
|
||||
/* LDRA_EXCLUDE 170 S <justification start> FALSE POSITIVE -- needed for ncurses <justification end> */
|
||||
W = initscr(); /* Determine terminal type and initialize curses data structures */
|
||||
/* LDRA_EXCLUDE 170 S <justification start> FALSE POSITIVE -- needed for ncurses <justification end> */
|
||||
start_color(); /* Enable use of color with ncurses */
|
||||
/* LDRA_EXCLUDE 170 S <justification start> FALSE POSITIVE -- needed for ncurses <justification end> */
|
||||
cbreak(); /* Disable line buffering and make char inputs immediately accessible to program */
|
||||
/* LDRA_EXCLUDE 170 S <justification start> FALSE POSITIVE -- needed for ncurses <justification end> */
|
||||
noecho(); /* Disable echo printing of chars type by user */
|
||||
if (W == NULL)
|
||||
{
|
||||
return 0;
|
||||
}
|
||||
/* LDRA_EXCLUDE 170 S <justification start> FALSE POSITIVE -- needed for ncurses <justification end> */
|
||||
nodelay(W, true); /* Make getch( ) a non-blocking call */
|
||||
/* LDRA_EXCLUDE 170 S <justification start> FALSE POSITIVE -- needed for ncurses <justification end> */
|
||||
keypad(W, true); /* Enable keypad and arrow keys */
|
||||
/* LDRA_EXCLUDE 170 S <justification start> FALSE POSITIVE -- needed for ncurses <justification end> */
|
||||
curs_set(0); /* Set cursor to be invisible - 0 */
|
||||
/* LDRA_EXCLUDE 170 S <justification start> FALSE POSITIVE -- needed for ncurses <justification end> */
|
||||
getmaxyx(W, rowLimit, colLimit); /* Query terminal dimensions */
|
||||
|
||||
/* Define color pallete foreground-background color pairs */
|
||||
/* PairID#, Foreground Color, Background Color */
|
||||
/* Foreground color == Background color ==> solid color block */
|
||||
init_pair(1, COLOR_BLACK, COLOR_BLACK); /* Black text on Black background */
|
||||
init_pair(2, COLOR_GREEN, COLOR_GREEN); /* Red text on Black background */
|
||||
/* LDRA_EXCLUDE 170 S <justification start> FALSE POSITIVE -- needed for ncurses <justification end> */
|
||||
init_pair(3, COLOR_WHITE, COLOR_WHITE); /* White text on Blue background */
|
||||
/* LDRA_EXCLUDE 170 S <justification start> FALSE POSITIVE -- needed for ncurses <justification end> */
|
||||
init_pair(4, COLOR_RED, COLOR_RED); /* Yellow text on Yellow background */
|
||||
/* LDRA_EXCLUDE 170 S <justification start> FALSE POSITIVE -- needed for ncurses <justification end> */
|
||||
init_pair(5, COLOR_YELLOW, COLOR_YELLOW); /* Yellow text on Yellow background */
|
||||
|
||||
|
||||
for (c1 =(unsigned)0; c1 < row; c1++)
|
||||
{
|
||||
/*printw ("\n");*/
|
||||
for (c2=(unsigned)0; c2 < col; c2++)
|
||||
{
|
||||
if (maze==NULL)
|
||||
{
|
||||
break;
|
||||
}
|
||||
switch(maze[c1][c2])
|
||||
{
|
||||
case '#':
|
||||
/* read array values, dynamically allocate array*/
|
||||
/* LDRA_EXCLUDE 170 S <justification start> FALSE POSITIVE -- needed for ncurses <justification end> */
|
||||
/* LDRA_EXCLUDE 170 S <justification start> FALSE POSITIVE -- needed for ncurses <justification end> */
|
||||
attron(COLOR_PAIR(2));
|
||||
/* LDRA_EXCLUDE 170 S <justification start> FALSE POSITIVE -- needed for ncurses <justification end> */
|
||||
wmove(W, c1,c2);
|
||||
/* LDRA_EXCLUDE 170 S <justification start> FALSE POSITIVE -- needed for ncurses <justification end> */
|
||||
waddch(W,'#');
|
||||
/* LDRA_EXCLUDE 170 S <justification start> FALSE POSITIVE -- needed for ncurses <justification end> */
|
||||
/* LDRA_EXCLUDE 170 S <justification start> FALSE POSITIVE -- needed for ncurses <justification end> */
|
||||
attroff(COLOR_PAIR(2));
|
||||
break;
|
||||
case ' ':
|
||||
/* LDRA_EXCLUDE 170 S <justification start> FALSE POSITIVE -- needed for ncurses <justification end> */
|
||||
wmove(W, c1,c2);
|
||||
/* LDRA_EXCLUDE 170 S <justification start> FALSE POSITIVE -- needed for ncurses <justification end> */
|
||||
waddch(W,' ');
|
||||
break;
|
||||
case 'P':
|
||||
/* LDRA_EXCLUDE 170 S <justification start> FALSE POSITIVE -- needed for ncurses <justification end> */
|
||||
wmove(W, c1,c2);
|
||||
/* LDRA_EXCLUDE 170 S <justification start> FALSE POSITIVE -- needed for ncurses <justification end> */
|
||||
waddch(W,'P');
|
||||
prow=c1;
|
||||
pcol=c2;
|
||||
break;
|
||||
case 'Z':
|
||||
/* LDRA_EXCLUDE 170 S <justification start> FALSE POSITIVE -- needed for ncurses <justification end> */
|
||||
/* LDRA_EXCLUDE 170 S <justification start> FALSE POSITIVE -- needed for ncurses <justification end> */
|
||||
attron(COLOR_PAIR(4));
|
||||
/* LDRA_EXCLUDE 170 S <justification start> FALSE POSITIVE -- needed for ncurses <justification end> */
|
||||
wmove(W, c1,c2);
|
||||
/* LDRA_EXCLUDE 170 S <justification start> FALSE POSITIVE -- needed for ncurses <justification end> */
|
||||
waddch(W,'Z');
|
||||
/* LDRA_EXCLUDE 170 S <justification start> FALSE POSITIVE -- needed for ncurses <justification end> */
|
||||
/* LDRA_EXCLUDE 170 S <justification start> FALSE POSITIVE -- needed for ncurses <justification end> */
|
||||
attroff(COLOR_PAIR(4));
|
||||
break;
|
||||
case 'T':
|
||||
/* LDRA_EXCLUDE 170 S <justification start> FALSE POSITIVE -- needed for ncurses <justification end> */
|
||||
/* LDRA_EXCLUDE 170 S <justification start> FALSE POSITIVE -- needed for ncurses <justification end> */
|
||||
attron(COLOR_PAIR(5));
|
||||
/* LDRA_EXCLUDE 170 S <justification start> FALSE POSITIVE -- needed for ncurses <justification end> */
|
||||
wmove(W, c1,c2);
|
||||
/* LDRA_EXCLUDE 170 S <justification start> FALSE POSITIVE -- needed for ncurses <justification end> */
|
||||
waddch(W,'T');
|
||||
/* LDRA_EXCLUDE 170 S <justification start> FALSE POSITIVE -- needed for ncurses <justification end> */
|
||||
/* LDRA_EXCLUDE 170 S <justification start> FALSE POSITIVE -- needed for ncurses <justification end> */
|
||||
attroff(COLOR_PAIR(5));
|
||||
break;
|
||||
|
||||
default:
|
||||
break;
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
deltaRow =0;
|
||||
deltaCol =0;
|
||||
|
||||
|
||||
|
||||
do{
|
||||
|
||||
/* LDRA_EXCLUDE 170 S <justification start> FALSE POSITIVE -- needed for ncurses <justification end> */
|
||||
int kb = wgetch(W); /* Grab keypress */
|
||||
|
||||
/* Support player movement via WASD, IJKL, and arrow keys */
|
||||
if (kb == (int)'a' || kb == (int)'j' || kb == KEY_LEFT)
|
||||
{
|
||||
/* Move Left */
|
||||
deltaRow =0;
|
||||
deltaCol =-1;
|
||||
}
|
||||
else if (kb == (int)'d' || kb == (int)'l' || kb == KEY_RIGHT)
|
||||
{
|
||||
/* Move Right */
|
||||
deltaRow =0;
|
||||
deltaCol =1;
|
||||
}
|
||||
else if (kb == (int)'w' || kb == (int)'i' || kb == KEY_UP)
|
||||
{
|
||||
/* Move Up */
|
||||
deltaRow =-1;
|
||||
deltaCol =0;
|
||||
}
|
||||
else if (kb == (int)'s' || kb == (int)'k' || kb == KEY_DOWN)
|
||||
{
|
||||
/* Move Down */
|
||||
deltaRow =1;
|
||||
deltaCol =0;
|
||||
}
|
||||
else if (kb == (int)'q')
|
||||
{
|
||||
/* q to exit player movement loop */
|
||||
break;
|
||||
};
|
||||
|
||||
/* LDRA_EXCLUDE 170 S <justification start> FALSE POSITIVE -- needed for ncurses <justification end> */
|
||||
/* LDRA_EXCLUDE 170 S <justification start> FALSE POSITIVE -- needed for ncurses <justification end> */
|
||||
attron(COLOR_PAIR(1));
|
||||
/* LDRA_EXCLUDE 170 S <justification start> FALSE POSITIVE -- needed for ncurses <justification end> */
|
||||
wmove(W,prow,pcol);
|
||||
/* LDRA_EXCLUDE 170 S <justification start> FALSE POSITIVE -- needed for ncurses <justification end> */
|
||||
waddch(W, 'P');
|
||||
/* LDRA_EXCLUDE 170 S <justification start> FALSE POSITIVE -- needed for ncurses <justification end> */
|
||||
/* LDRA_EXCLUDE 170 S <justification start> FALSE POSITIVE -- needed for ncurses <justification end> */
|
||||
attroff(COLOR_PAIR(1));
|
||||
|
||||
if(((prow + (unsigned int)deltaRow) > row) || ((pcol + (unsigned int)deltaCol) > col)){
|
||||
deltaRow = 0;
|
||||
deltaCol = 0;
|
||||
}
|
||||
if(maze[((int)prow + deltaRow)][((int)pcol + deltaCol)] == '#'){
|
||||
prow = prow;
|
||||
pcol = pcol;
|
||||
}
|
||||
else{
|
||||
prow = prow + (unsigned int)deltaRow;
|
||||
pcol = pcol + (unsigned int)deltaCol;
|
||||
}
|
||||
if(maze[prow][pcol] == 'T')
|
||||
{
|
||||
maze[prow][pcol] = ' ';
|
||||
score++;
|
||||
}
|
||||
if(maze[prow][pcol] == 'Z')
|
||||
{
|
||||
break;
|
||||
}
|
||||
|
||||
/* LDRA_EXCLUDE 170 S <justification start> FALSE POSITIVE -- needed for ncurses <justification end> */
|
||||
/* LDRA_EXCLUDE 170 S <justification start> FALSE POSITIVE -- needed for ncurses <justification end> */
|
||||
attron(COLOR_PAIR(3));
|
||||
/* LDRA_EXCLUDE 170 S <justification start> FALSE POSITIVE -- needed for ncurses <justification end> */
|
||||
wmove(W,prow,pcol);
|
||||
/* LDRA_EXCLUDE 170 S <justification start> FALSE POSITIVE -- needed for ncurses <justification end> */
|
||||
waddch(W, 'P');
|
||||
/* LDRA_EXCLUDE 170 S <justification start> FALSE POSITIVE -- needed for ncurses <justification end> */
|
||||
/* LDRA_EXCLUDE 170 S <justification start> FALSE POSITIVE -- needed for ncurses <justification end> */
|
||||
attroff(COLOR_PAIR(3));
|
||||
|
||||
deltaRow =0;
|
||||
deltaCol =0;
|
||||
|
||||
/* LDRA_EXCLUDE 170 S <justification start> FALSE POSITIVE -- needed for ncurses <justification end> */
|
||||
wrefresh(W); /* Refresh ncurses window */
|
||||
|
||||
} while (true); /* Repeat until user selects q to quit */
|
||||
|
||||
/* free the memory */
|
||||
for(y = (unsigned)0; y < row; y++)
|
||||
{
|
||||
free(maze[y]);
|
||||
maze[y] = NULL;
|
||||
}
|
||||
if(maze != NULL){
|
||||
free(maze);
|
||||
}
|
||||
/*printw("\n");*/
|
||||
/* LDRA_EXCLUDE 170 S <justification start> FALSE POSITIVE -- needed for ncurses <justification end> */
|
||||
endwin();
|
||||
printf("Score = %u\n", score);
|
||||
return 0;
|
||||
}
|
||||
|
@ -0,0 +1,278 @@
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <stdint.h>
|
||||
#include <string.h>
|
||||
#include <unistd.h>
|
||||
#include <ncurses.h>
|
||||
|
||||
|
||||
/*nothing*/
|
||||
|
||||
int main(int argc, char *argv[])
|
||||
{
|
||||
FILE* mazeFile; /* Maze Input File */
|
||||
unsigned int row = 0; /* Holds number of rows for input from file */
|
||||
unsigned int col = 0; /* Holds number of cols for input from file */
|
||||
unsigned int pcol = 0;
|
||||
unsigned int prow = 0;
|
||||
char shold[2000]; /* Holds lines for input from file */
|
||||
unsigned int l = 0;
|
||||
unsigned int k = 0;
|
||||
char **maze; /* array for maze */
|
||||
unsigned int c1 = 0;
|
||||
unsigned int c2 = 0;
|
||||
unsigned int i = 0;
|
||||
unsigned int j = 0;
|
||||
unsigned int x = 0;
|
||||
unsigned int y = 0;
|
||||
int deltaRow, deltaCol; /* Change in row-column position based on key input */
|
||||
unsigned int score = 0;
|
||||
|
||||
|
||||
WINDOW* W = 0; /* Curses Window handle */
|
||||
int rowLimit,colLimit; /* Num of rows and columns in terminal window */
|
||||
/* Initialize ncurses -- you will want to use the following settings */
|
||||
|
||||
|
||||
|
||||
/* check to see if argument */
|
||||
if (argc != 2)
|
||||
{
|
||||
/*printw("An error has ocurred no input file was given.\n");*/
|
||||
return 1;
|
||||
}
|
||||
mazeFile = fopen(argv[1], "r");
|
||||
if (mazeFile == NULL)
|
||||
{
|
||||
/*printw("Error in opening the file.\n");*/
|
||||
return 1;
|
||||
}
|
||||
fgets(shold, 2, mazeFile);
|
||||
while ( feof(mazeFile) == 0 )
|
||||
{
|
||||
|
||||
|
||||
switch(shold[0])
|
||||
{
|
||||
case 'M':
|
||||
/* read array values, dynamically allocate array */
|
||||
fscanf(mazeFile, "%3u %3u", &row, &col);
|
||||
maze = (char**)malloc(row * 8);
|
||||
for(i = (unsigned) 0; i < row; i++)
|
||||
{
|
||||
if (maze==NULL)
|
||||
{
|
||||
break;
|
||||
}
|
||||
maze[i] = malloc(col * 8);
|
||||
}
|
||||
for (x = (unsigned) 0; x < row; x++)
|
||||
{
|
||||
for (j = (unsigned) 0; j < col; j++)
|
||||
{
|
||||
fscanf(mazeFile, "%c", &maze[x][j]);
|
||||
if (maze[x][j] == '\n')
|
||||
{
|
||||
fscanf(mazeFile, "%c", &maze[x][j]);
|
||||
}
|
||||
}
|
||||
}
|
||||
break;
|
||||
case 'P':
|
||||
fscanf(mazeFile, "%3u %3u", &k, &l);
|
||||
/* check maze bounds */
|
||||
if(k>= row || l>= col){
|
||||
break;
|
||||
}
|
||||
maze[k][l] = 'P';
|
||||
break;
|
||||
|
||||
case 'Z':
|
||||
fscanf(mazeFile, "%3u %3u", &k, &l);
|
||||
/* check maze bounds */
|
||||
if(k>= row || l>= col){
|
||||
break;
|
||||
}
|
||||
maze[k][l] = 'Z';
|
||||
break;
|
||||
|
||||
case 'T':
|
||||
fscanf(mazeFile, "%3u %3u", &k, &l);
|
||||
/* check maze bounds */
|
||||
if(k>= row || l>= col){
|
||||
break;
|
||||
}
|
||||
maze[k][l] = 'T';
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
};
|
||||
fgets(shold, 2, mazeFile);
|
||||
if (feof(mazeFile) != 0)
|
||||
{
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
fclose(mazeFile);
|
||||
W = initscr(); /* Determine terminal type and initialize curses data structures */
|
||||
start_color(); /* Enable use of color with ncurses */
|
||||
cbreak(); /* Disable line buffering and make char inputs immediately accessible to program */
|
||||
noecho(); /* Disable echo printing of chars type by user */
|
||||
nodelay(W, true); /* Make getch( ) a non-blocking call */
|
||||
keypad(W, true); /* Enable keypad and arrow keys */
|
||||
curs_set(0); /* Set cursor to be invisible - 0 */
|
||||
getmaxyx(W, rowLimit, colLimit); /* Query terminal dimensions */
|
||||
|
||||
/* Define color pallete foreground-background color pairs */
|
||||
/* PairID#, Foreground Color, Background Color */
|
||||
/* Foreground color == Background color ==> solid color block */
|
||||
init_pair(1, COLOR_BLACK, COLOR_BLACK); /* Black text on Black background */
|
||||
init_pair(2, COLOR_GREEN, COLOR_GREEN); /* Red text on Black background */
|
||||
init_pair(3, COLOR_WHITE, COLOR_WHITE); /* White text on Blue background */
|
||||
init_pair(4, COLOR_RED, COLOR_RED); /* Yellow text on Yellow background */
|
||||
init_pair(5, COLOR_YELLOW, COLOR_YELLOW); /* Yellow text on Yellow background */
|
||||
|
||||
|
||||
for (c1 =(unsigned)0; c1 < row; c1++)
|
||||
{
|
||||
/*printw ("\n");*/
|
||||
for (c2=(unsigned)0; c2 < col; c2++)
|
||||
{
|
||||
if (maze==NULL)
|
||||
{
|
||||
break;
|
||||
}
|
||||
switch(maze[c1][c2])
|
||||
{
|
||||
case '#':
|
||||
/* read array values, dynamically allocate array*/
|
||||
attron(COLOR_PAIR(2));
|
||||
wmove(W, c1,c2);
|
||||
waddch(W,'#');
|
||||
attroff(COLOR_PAIR(2));
|
||||
break;
|
||||
case ' ':
|
||||
wmove(W, c1,c2);
|
||||
waddch(W,' ');
|
||||
break;
|
||||
case 'P':
|
||||
wmove(W, c1,c2);
|
||||
waddch(W,'P');
|
||||
prow=c1;
|
||||
pcol=c2;
|
||||
break;
|
||||
case 'Z':
|
||||
attron(COLOR_PAIR(4));
|
||||
wmove(W, c1,c2);
|
||||
waddch(W,'Z');
|
||||
attroff(COLOR_PAIR(4));
|
||||
break;
|
||||
case 'T':
|
||||
attron(COLOR_PAIR(5));
|
||||
wmove(W, c1,c2);
|
||||
waddch(W,'T');
|
||||
attroff(COLOR_PAIR(5));
|
||||
break;
|
||||
|
||||
default:
|
||||
break;
|
||||
};
|
||||
/*printw("%c", maze[c1][c2]);*/
|
||||
}
|
||||
}
|
||||
|
||||
deltaRow =0;
|
||||
deltaCol =0;
|
||||
|
||||
|
||||
|
||||
do{
|
||||
|
||||
int kb = wgetch(W); /* Grab keypress */
|
||||
|
||||
/* Support player movement via WASD, IJKL, and arrow keys */
|
||||
if (kb == 'a' || kb == 'j' || kb == KEY_LEFT)
|
||||
{
|
||||
/* Move Left */
|
||||
deltaRow =0;
|
||||
deltaCol =-1;
|
||||
}
|
||||
else if (kb == 'd' || kb == 'l' || kb == KEY_RIGHT)
|
||||
{
|
||||
/* Move Right */
|
||||
deltaRow =0;
|
||||
deltaCol =1;
|
||||
}
|
||||
else if (kb == 'w' || kb == 'i' || kb == KEY_UP)
|
||||
{
|
||||
/* Move Up */
|
||||
deltaRow =-1;
|
||||
deltaCol =0;
|
||||
}
|
||||
else if (kb == 's' || kb == 'k' || kb == KEY_DOWN)
|
||||
{
|
||||
/* Move Down */
|
||||
deltaRow =1;
|
||||
deltaCol =0;
|
||||
}
|
||||
else if (kb == 'q')
|
||||
{
|
||||
/* q to exit player movement loop */
|
||||
break;
|
||||
};
|
||||
|
||||
attron(COLOR_PAIR(1));
|
||||
wmove(W,prow,pcol);
|
||||
waddch(W, 'P');
|
||||
attroff(COLOR_PAIR(1));
|
||||
|
||||
if((prow + deltaRow > row) || (pcol + deltaCol > col)){
|
||||
deltaRow = 0;
|
||||
deltaCol = 0;
|
||||
}
|
||||
if(maze[prow + deltaRow][pcol + deltaCol] == '#'){
|
||||
prow = prow;
|
||||
pcol = pcol;
|
||||
}
|
||||
else{
|
||||
prow = prow + deltaRow;
|
||||
pcol = pcol + deltaCol;
|
||||
}
|
||||
if(maze[prow][pcol] == 'T')
|
||||
{
|
||||
score++;
|
||||
}
|
||||
if(maze[prow][pcol] == 'Z')
|
||||
{
|
||||
break;
|
||||
}
|
||||
|
||||
attron(COLOR_PAIR(3));
|
||||
wmove(W,prow,pcol);
|
||||
waddch(W, 'P');
|
||||
attroff(COLOR_PAIR(3));
|
||||
|
||||
deltaRow =0;
|
||||
deltaCol =0;
|
||||
|
||||
wrefresh(W); /* Refresh ncurses window */
|
||||
|
||||
} while (true); /* Repeat until user selects q to quit */
|
||||
|
||||
|
||||
/* free the memory */
|
||||
for(y = (unsigned)0; y < row; y++)
|
||||
{
|
||||
free(maze[y]);
|
||||
maze[y] = NULL;
|
||||
}
|
||||
if(maze != NULL){
|
||||
free(maze);
|
||||
}
|
||||
/*printw("\n");*/
|
||||
endwin();
|
||||
return 0;
|
||||
}
|
||||
|
@ -0,0 +1,7 @@
|
||||
#include <stdio.h>
|
||||
|
||||
int main(int argc, char const *argv[])
|
||||
{
|
||||
printf("Hello, World!\n");
|
||||
return 0;
|
||||
}
|
@ -0,0 +1,401 @@
|
||||
#include <ncurses.h>
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <stdint.h>
|
||||
#include <string.h>
|
||||
#include <unistd.h>
|
||||
|
||||
extern WINDOW* initscr(void);
|
||||
extern int start_color(void);
|
||||
extern int noecho(void);
|
||||
|
||||
|
||||
/* LDRA_EXCLUDE RULE VIOLATION <justification start> FALSE POSITIVE -- why <justification end> */
|
||||
|
||||
int main(int argc, char *argv[])
|
||||
{
|
||||
FILE* mazeFile; /* Maze Input File */
|
||||
unsigned int row = 0; /* Holds number of rows for input from file */
|
||||
unsigned int col = 0; /* Holds number of cols for input from file */
|
||||
unsigned int pcol = 0;
|
||||
unsigned int prow = 0;
|
||||
char shold[2000]; /* Holds lines for input from file */
|
||||
unsigned int l = 0;
|
||||
unsigned int k = 0;
|
||||
char **maze; /* array for maze */
|
||||
unsigned int c1 = 0;
|
||||
unsigned int c2 = 0;
|
||||
unsigned int i = 0;
|
||||
unsigned int j = 0;
|
||||
unsigned int x = 0;
|
||||
unsigned int y = 0;
|
||||
int deltaRow, deltaCol; /* Change in row-column position based on key input */
|
||||
unsigned int score = 0;
|
||||
|
||||
|
||||
WINDOW* W = 0; /* Curses Window handle */
|
||||
unsigned int rowLimit,colLimit; /* Num of rows and columns in terminal window */
|
||||
/* Initialize ncurses -- you will want to use the following settings */
|
||||
|
||||
|
||||
|
||||
/* check to see if argument */
|
||||
if (argc != 2)
|
||||
{
|
||||
/*printw("An error has ocurred no input file was given.\n");*/
|
||||
return 1;
|
||||
}
|
||||
mazeFile = fopen(argv[1], "r");
|
||||
if (mazeFile == NULL)
|
||||
{
|
||||
/*printw("Error in opening the file.\n");*/
|
||||
return 1;
|
||||
}
|
||||
fgets(shold, 2, mazeFile);
|
||||
while ( feof(mazeFile) == 0 )
|
||||
{
|
||||
|
||||
|
||||
switch(shold[0])
|
||||
{
|
||||
case 'M':
|
||||
/* read array values, dynamically allocate array */
|
||||
fscanf(mazeFile, "%3u %3u", &row, &col);
|
||||
maze = (char**)malloc(row * 8); /* */
|
||||
for(i = (unsigned) 0; i < row; i++)
|
||||
{
|
||||
if (maze==NULL)
|
||||
{
|
||||
break;
|
||||
}
|
||||
maze[i] = malloc(col * 8);
|
||||
}
|
||||
for (x = (unsigned) 0; x < row; x++)
|
||||
{
|
||||
for (j = (unsigned) 0; j < col; j++)
|
||||
{
|
||||
fscanf(mazeFile, "%c", &maze[x][j]);
|
||||
if (maze[x][j] == '\n')
|
||||
{
|
||||
fscanf(mazeFile, "%c", &maze[x][j]);
|
||||
}
|
||||
}
|
||||
}
|
||||
break;
|
||||
case 'P':
|
||||
fscanf(mazeFile, "%3u %3u", &k, &l);
|
||||
/* check maze bounds */
|
||||
if(k>= row || l>= col){
|
||||
break;
|
||||
}
|
||||
maze[k][l] = 'P';
|
||||
break;
|
||||
|
||||
case 'Z':
|
||||
fscanf(mazeFile, "%3u %3u", &k, &l);
|
||||
/* check maze bounds */
|
||||
if(k>= row || l>= col){
|
||||
break;
|
||||
}
|
||||
maze[k][l] = 'Z';
|
||||
break;
|
||||
|
||||
case 'T':
|
||||
fscanf(mazeFile, "%3u %3u", &k, &l);
|
||||
/* check maze bounds */
|
||||
if(k>= row || l>= col){
|
||||
break;
|
||||
}
|
||||
maze[k][l] = 'T';
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
};
|
||||
fgets(shold, 2, mazeFile);
|
||||
if (feof(mazeFile) != 0)
|
||||
{
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
rowLimit = row;
|
||||
colLimit = col;
|
||||
fclose(mazeFile);
|
||||
/* LDRA_EXCLUDE 170 S <justification start> FALSE POSITIVE -- needed for ncurses <justification end> */
|
||||
/* LDRA_EXCLUDE 496 S <justification start> FALSE POSITIVE -- needed for ncurses <justification end> */
|
||||
/* LDRA_EXCLUDE 41 D <justification start> FALSE POSITIVE -- needed for ncurses <justification end> */
|
||||
W = initscr(); /* Determine terminal type and initialize curses data structures */
|
||||
/* LDRA_EXCLUDE 170 S <justification start> FALSE POSITIVE -- needed for ncurses <justification end> */
|
||||
/* LDRA_EXCLUDE 496 S <justification start> FALSE POSITIVE -- needed for ncurses <justification end> */
|
||||
/* LDRA_EXCLUDE 41 D <justification start> FALSE POSITIVE -- needed for ncurses <justification end> */
|
||||
start_color(); /* Enable use of color with ncurses */
|
||||
/* LDRA_EXCLUDE 170 S <justification start> FALSE POSITIVE -- needed for ncurses <justification end> */
|
||||
/* LDRA_EXCLUDE 496 S <justification start> FALSE POSITIVE -- needed for ncurses <justification end> */
|
||||
/* LDRA_EXCLUDE 41 D <justification start> FALSE POSITIVE -- needed for ncurses <justification end> */
|
||||
cbreak(); /* Disable line buffering and make char inputs immediately accessible to program */
|
||||
/* LDRA_EXCLUDE 170 S <justification start> FALSE POSITIVE -- needed for ncurses <justification end> */
|
||||
/* LDRA_EXCLUDE 496 S <justification start> FALSE POSITIVE -- needed for ncurses <justification end> */
|
||||
/* LDRA_EXCLUDE 41 D <justification start> FALSE POSITIVE -- needed for ncurses <justification end> */
|
||||
noecho(); /* Disable echo printing of chars type by user */
|
||||
if (W == NULL)
|
||||
{
|
||||
return 0;
|
||||
}
|
||||
/* LDRA_EXCLUDE 170 S <justification start> FALSE POSITIVE -- needed for ncurses <justification end> */
|
||||
/* LDRA_EXCLUDE 496 S <justification start> FALSE POSITIVE -- needed for ncurses <justification end> */
|
||||
/* LDRA_EXCLUDE 41 D <justification start> FALSE POSITIVE -- needed for ncurses <justification end> */
|
||||
nodelay(W, true); /* Make getch( ) a non-blocking call */
|
||||
/* LDRA_EXCLUDE 170 S <justification start> FALSE POSITIVE -- needed for ncurses <justification end> */
|
||||
/* LDRA_EXCLUDE 496 S <justification start> FALSE POSITIVE -- needed for ncurses <justification end> */
|
||||
/* LDRA_EXCLUDE 41 D <justification start> FALSE POSITIVE -- needed for ncurses <justification end> */
|
||||
keypad(W, true); /* Enable keypad and arrow keys */
|
||||
/* LDRA_EXCLUDE 170 S <justification start> FALSE POSITIVE -- needed for ncurses <justification end> */
|
||||
/* LDRA_EXCLUDE 496 S <justification start> FALSE POSITIVE -- needed for ncurses <justification end> */
|
||||
/* LDRA_EXCLUDE 41 D <justification start> FALSE POSITIVE -- needed for ncurses <justification end> */
|
||||
curs_set(0); /* Set cursor to be invisible - 0 */
|
||||
/* LDRA_EXCLUDE 170 S <justification start> FALSE POSITIVE -- needed for ncurses <justification end> */
|
||||
/* LDRA_EXCLUDE 496 S <justification start> FALSE POSITIVE -- needed for ncurses <justification end> */
|
||||
/* LDRA_EXCLUDE 41 D <justification start> FALSE POSITIVE -- needed for ncurses <justification end> */
|
||||
getmaxyx(W, rowLimit, colLimit); /* Query terminal dimensions */
|
||||
|
||||
/* Define color pallete foreground-background color pairs */
|
||||
/* PairID#, Foreground Color, Background Color */
|
||||
/* Foreground color == Background color ==> solid color block */
|
||||
/* LDRA_EXCLUDE 170 S <justification start> FALSE POSITIVE -- needed for ncurses <justification end> */
|
||||
/* LDRA_EXCLUDE 496 S <justification start> FALSE POSITIVE -- needed for ncurses <justification end> */
|
||||
/* LDRA_EXCLUDE 41 D <justification start> FALSE POSITIVE -- needed for ncurses <justification end> */
|
||||
init_pair(1, COLOR_BLACK, COLOR_BLACK); /* Black text on Black background */
|
||||
/* LDRA_EXCLUDE 170 S <justification start> FALSE POSITIVE -- needed for ncurses <justification end> */
|
||||
/* LDRA_EXCLUDE 496 S <justification start> FALSE POSITIVE -- needed for ncurses <justification end> */
|
||||
init_pair(2, COLOR_GREEN, COLOR_GREEN); /* Red text on Black background */
|
||||
/* LDRA_EXCLUDE 170 S <justification start> FALSE POSITIVE -- needed for ncurses <justification end> */
|
||||
/* LDRA_EXCLUDE 496 S <justification start> FALSE POSITIVE -- needed for ncurses <justification end> */
|
||||
init_pair(3, COLOR_WHITE, COLOR_WHITE); /* White text on Blue background */
|
||||
/* LDRA_EXCLUDE 170 S <justification start> FALSE POSITIVE -- needed for ncurses <justification end> */
|
||||
/* LDRA_EXCLUDE 496 S <justification start> FALSE POSITIVE -- needed for ncurses <justification end> */
|
||||
init_pair(4, COLOR_RED, COLOR_RED); /* Yellow text on Yellow background */
|
||||
/* LDRA_EXCLUDE 170 S <justification start> FALSE POSITIVE -- needed for ncurses <justification end> */
|
||||
/* LDRA_EXCLUDE 496 S <justification start> FALSE POSITIVE -- needed for ncurses <justification end> */
|
||||
init_pair(5, COLOR_YELLOW, COLOR_YELLOW); /* Yellow text on Yellow background */
|
||||
|
||||
|
||||
for (c1 =(unsigned)0; c1 < row; c1++)
|
||||
{
|
||||
/*printw ("\n");*/
|
||||
for (c2=(unsigned)0; c2 < col; c2++)
|
||||
{
|
||||
if (maze==NULL)
|
||||
{
|
||||
break;
|
||||
}
|
||||
switch(maze[c1][c2])
|
||||
{
|
||||
case '#':
|
||||
/* read array values, dynamically allocate array*/
|
||||
/* LDRA_EXCLUDE 170 S <justification start> FALSE POSITIVE -- needed for ncurses <justification end> */
|
||||
/* LDRA_EXCLUDE 170 S <justification start> FALSE POSITIVE -- needed for ncurses <justification end> */
|
||||
/* LDRA_EXCLUDE 496 S <justification start> FALSE POSITIVE -- needed for ncurses <justification end> */
|
||||
/* LDRA_EXCLUDE 496 S <justification start> FALSE POSITIVE -- needed for ncurses <justification end> */
|
||||
/* LDRA_EXCLUDE 41 D <justification start> FALSE POSITIVE -- needed for ncurses <justification end> */
|
||||
/* LDRA_EXCLUDE 41 D <justification start> FALSE POSITIVE -- needed for ncurses <justification end> */
|
||||
attron(COLOR_PAIR(2));
|
||||
/* LDRA_EXCLUDE 170 S <justification start> FALSE POSITIVE -- needed for ncurses <justification end> */
|
||||
/* LDRA_EXCLUDE 496 S <justification start> FALSE POSITIVE -- needed for ncurses <justification end> */
|
||||
wmove(W, c1,c2);
|
||||
/* LDRA_EXCLUDE 170 S <justification start> FALSE POSITIVE -- needed for ncurses <justification end> */
|
||||
/* LDRA_EXCLUDE 496 S <justification start> FALSE POSITIVE -- needed for ncurses <justification end> */
|
||||
waddch(W,'#');
|
||||
/* LDRA_EXCLUDE 170 S <justification start> FALSE POSITIVE -- needed for ncurses <justification end> */
|
||||
/* LDRA_EXCLUDE 170 S <justification start> FALSE POSITIVE -- needed for ncurses <justification end> */
|
||||
/* LDRA_EXCLUDE 496 S <justification start> FALSE POSITIVE -- needed for ncurses <justification end> */
|
||||
/* LDRA_EXCLUDE 496 S <justification start> FALSE POSITIVE -- needed for ncurses <justification end> */
|
||||
/* LDRA_EXCLUDE 41 D <justification start> FALSE POSITIVE -- needed for ncurses <justification end> */
|
||||
attroff(COLOR_PAIR(2));
|
||||
break;
|
||||
case ' ':
|
||||
/* LDRA_EXCLUDE 170 S <justification start> FALSE POSITIVE -- needed for ncurses <justification end> */
|
||||
/* LDRA_EXCLUDE 496 S <justification start> FALSE POSITIVE -- needed for ncurses <justification end> */
|
||||
wmove(W, c1,c2);
|
||||
/* LDRA_EXCLUDE 170 S <justification start> FALSE POSITIVE -- needed for ncurses <justification end> */
|
||||
/* LDRA_EXCLUDE 496 S <justification start> FALSE POSITIVE -- needed for ncurses <justification end> */
|
||||
waddch(W,' ');
|
||||
break;
|
||||
case 'P':
|
||||
/* LDRA_EXCLUDE 170 S <justification start> FALSE POSITIVE -- needed for ncurses <justification end> */
|
||||
/* LDRA_EXCLUDE 496 S <justification start> FALSE POSITIVE -- needed for ncurses <justification end> */
|
||||
wmove(W, c1,c2);
|
||||
/* LDRA_EXCLUDE 170 S <justification start> FALSE POSITIVE -- needed for ncurses <justification end> */
|
||||
/* LDRA_EXCLUDE 496 S <justification start> FALSE POSITIVE -- needed for ncurses <justification end> */
|
||||
waddch(W,'P');
|
||||
prow=c1;
|
||||
pcol=c2;
|
||||
break;
|
||||
case 'Z':
|
||||
/* LDRA_EXCLUDE 170 S <justification start> FALSE POSITIVE -- needed for ncurses <justification end> */
|
||||
/* LDRA_EXCLUDE 170 S <justification start> FALSE POSITIVE -- needed for ncurses <justification end> */
|
||||
/* LDRA_EXCLUDE 496 S <justification start> FALSE POSITIVE -- needed for ncurses <justification end> */
|
||||
/* LDRA_EXCLUDE 496 S <justification start> FALSE POSITIVE -- needed for ncurses <justification end> */
|
||||
attron(COLOR_PAIR(4));
|
||||
/* LDRA_EXCLUDE 170 S <justification start> FALSE POSITIVE -- needed for ncurses <justification end> */
|
||||
/* LDRA_EXCLUDE 496 S <justification start> FALSE POSITIVE -- needed for ncurses <justification end> */
|
||||
wmove(W, c1,c2);
|
||||
/* LDRA_EXCLUDE 170 S <justification start> FALSE POSITIVE -- needed for ncurses <justification end> */
|
||||
/* LDRA_EXCLUDE 496 S <justification start> FALSE POSITIVE -- needed for ncurses <justification end> */
|
||||
waddch(W,'Z');
|
||||
/* LDRA_EXCLUDE 170 S <justification start> FALSE POSITIVE -- needed for ncurses <justification end> */
|
||||
/* LDRA_EXCLUDE 170 S <justification start> FALSE POSITIVE -- needed for ncurses <justification end> */
|
||||
/* LDRA_EXCLUDE 496 S <justification start> FALSE POSITIVE -- needed for ncurses <justification end> */
|
||||
/* LDRA_EXCLUDE 496 S <justification start> FALSE POSITIVE -- needed for ncurses <justification end> */
|
||||
attroff(COLOR_PAIR(4));
|
||||
break;
|
||||
case 'T':
|
||||
/* LDRA_EXCLUDE 170 S <justification start> FALSE POSITIVE -- needed for ncurses <justification end> */
|
||||
/* LDRA_EXCLUDE 170 S <justification start> FALSE POSITIVE -- needed for ncurses <justification end> */
|
||||
/* LDRA_EXCLUDE 496 S <justification start> FALSE POSITIVE -- needed for ncurses <justification end> */
|
||||
/* LDRA_EXCLUDE 496 S <justification start> FALSE POSITIVE -- needed for ncurses <justification end> */
|
||||
attron(COLOR_PAIR(5));
|
||||
/* LDRA_EXCLUDE 170 S <justification start> FALSE POSITIVE -- needed for ncurses <justification end> */
|
||||
/* LDRA_EXCLUDE 496 S <justification start> FALSE POSITIVE -- needed for ncurses <justification end> */
|
||||
wmove(W, c1,c2);
|
||||
/* LDRA_EXCLUDE 170 S <justification start> FALSE POSITIVE -- needed for ncurses <justification end> */
|
||||
/* LDRA_EXCLUDE 496 S <justification start> FALSE POSITIVE -- needed for ncurses <justification end> */
|
||||
waddch(W,'T');
|
||||
/* LDRA_EXCLUDE 170 S <justification start> FALSE POSITIVE -- needed for ncurses <justification end> */
|
||||
/* LDRA_EXCLUDE 170 S <justification start> FALSE POSITIVE -- needed for ncurses <justification end> */
|
||||
/* LDRA_EXCLUDE 496 S <justification start> FALSE POSITIVE -- needed for ncurses <justification end> */
|
||||
/* LDRA_EXCLUDE 496 S <justification start> FALSE POSITIVE -- needed for ncurses <justification end> */
|
||||
attroff(COLOR_PAIR(5));
|
||||
break;
|
||||
|
||||
default:
|
||||
break;
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
deltaRow =0;
|
||||
deltaCol =0;
|
||||
|
||||
|
||||
|
||||
do{
|
||||
|
||||
/* LDRA_EXCLUDE 170 S <justification start> FALSE POSITIVE -- needed for ncurses <justification end> */
|
||||
/* LDRA_EXCLUDE 496 S <justification start> FALSE POSITIVE -- needed for ncurses <justification end> */
|
||||
int kb = wgetch(W); /* Grab keypress */
|
||||
|
||||
/* Support player movement via WASD, IJKL, and arrow keys */
|
||||
if (kb == (int)'a' || kb == (int)'j' || kb == KEY_LEFT)
|
||||
{
|
||||
/* Move Left */
|
||||
deltaRow =0;
|
||||
deltaCol =-1;
|
||||
}
|
||||
else if (kb == (int)'d' || kb == (int)'l' || kb == KEY_RIGHT)
|
||||
{
|
||||
/* Move Right */
|
||||
deltaRow =0;
|
||||
deltaCol =1;
|
||||
}
|
||||
else if (kb == (int)'w' || kb == (int)'i' || kb == KEY_UP)
|
||||
{
|
||||
/* Move Up */
|
||||
deltaRow =-1;
|
||||
deltaCol =0;
|
||||
}
|
||||
else if (kb == (int)'s' || kb == (int)'k' || kb == KEY_DOWN)
|
||||
{
|
||||
/* Move Down */
|
||||
deltaRow =1;
|
||||
deltaCol =0;
|
||||
}
|
||||
else if (kb == (int)'q')
|
||||
{
|
||||
/* q to exit player movement loop */
|
||||
break;
|
||||
};
|
||||
|
||||
/* LDRA_EXCLUDE 170 S <justification start> FALSE POSITIVE -- needed for ncurses <justification end> */
|
||||
/* LDRA_EXCLUDE 170 S <justification start> FALSE POSITIVE -- needed for ncurses <justification end> */
|
||||
/* LDRA_EXCLUDE 496 S <justification start> FALSE POSITIVE -- needed for ncurses <justification end> */
|
||||
/* LDRA_EXCLUDE 496 S <justification start> FALSE POSITIVE -- needed for ncurses <justification end> */
|
||||
attron(COLOR_PAIR(1));
|
||||
/* LDRA_EXCLUDE 170 S <justification start> FALSE POSITIVE -- needed for ncurses <justification end> */
|
||||
/* LDRA_EXCLUDE 496 S <justification start> FALSE POSITIVE -- needed for ncurses <justification end> */
|
||||
wmove(W,prow,pcol);
|
||||
/* LDRA_EXCLUDE 170 S <justification start> FALSE POSITIVE -- needed for ncurses <justification end> */
|
||||
/* LDRA_EXCLUDE 496 S <justification start> FALSE POSITIVE -- needed for ncurses <justification end> */
|
||||
waddch(W, 'P');
|
||||
/* LDRA_EXCLUDE 170 S <justification start> FALSE POSITIVE -- needed for ncurses <justification end> */
|
||||
/* LDRA_EXCLUDE 170 S <justification start> FALSE POSITIVE -- needed for ncurses <justification end> */
|
||||
/* LDRA_EXCLUDE 496 S <justification start> FALSE POSITIVE -- needed for ncurses <justification end> */
|
||||
/* LDRA_EXCLUDE 496 S <justification start> FALSE POSITIVE -- needed for ncurses <justification end> */
|
||||
attroff(COLOR_PAIR(1));
|
||||
|
||||
if(((prow + (unsigned int)deltaRow) > row) || ((pcol + (unsigned int)deltaCol) > col)){
|
||||
deltaRow = 0;
|
||||
deltaCol = 0;
|
||||
}
|
||||
if(maze[((int)prow + deltaRow)][((int)pcol + deltaCol)] == '#'){
|
||||
prow = prow;
|
||||
pcol = pcol;
|
||||
}
|
||||
else{
|
||||
prow = prow + (unsigned int)deltaRow;
|
||||
pcol = pcol + (unsigned int)deltaCol;
|
||||
}
|
||||
if(maze[prow][pcol] == 'T')
|
||||
{
|
||||
maze[prow][pcol] = ' ';
|
||||
score++;
|
||||
}
|
||||
if(maze[prow][pcol] == 'Z')
|
||||
{
|
||||
break;
|
||||
}
|
||||
|
||||
/* LDRA_EXCLUDE 170 S <justification start> FALSE POSITIVE -- needed for ncurses <justification end> */
|
||||
/* LDRA_EXCLUDE 170 S <justification start> FALSE POSITIVE -- needed for ncurses <justification end> */
|
||||
/* LDRA_EXCLUDE 496 S <justification start> FALSE POSITIVE -- needed for ncurses <justification end> */
|
||||
/* LDRA_EXCLUDE 496 S <justification start> FALSE POSITIVE -- needed for ncurses <justification end> */
|
||||
attron(COLOR_PAIR(3));
|
||||
/* LDRA_EXCLUDE 170 S <justification start> FALSE POSITIVE -- needed for ncurses <justification end> */
|
||||
/* LDRA_EXCLUDE 496 S <justification start> FALSE POSITIVE -- needed for ncurses <justification end> */
|
||||
wmove(W,prow,pcol);
|
||||
/* LDRA_EXCLUDE 170 S <justification start> FALSE POSITIVE -- needed for ncurses <justification end> */
|
||||
/* LDRA_EXCLUDE 496 S <justification start> FALSE POSITIVE -- needed for ncurses <justification end> */
|
||||
waddch(W, 'P');
|
||||
/* LDRA_EXCLUDE 170 S <justification start> FALSE POSITIVE -- needed for ncurses <justification end> */
|
||||
/* LDRA_EXCLUDE 170 S <justification start> FALSE POSITIVE -- needed for ncurses <justification end> */
|
||||
/* LDRA_EXCLUDE 496 S <justification start> FALSE POSITIVE -- needed for ncurses <justification end> */
|
||||
/* LDRA_EXCLUDE 496 S <justification start> FALSE POSITIVE -- needed for ncurses <justification end> */
|
||||
attroff(COLOR_PAIR(3));
|
||||
|
||||
deltaRow =0;
|
||||
deltaCol =0;
|
||||
|
||||
/* LDRA_EXCLUDE 170 S <justification start> FALSE POSITIVE -- needed for ncurses <justification end> */
|
||||
/* LDRA_EXCLUDE 496 S <justification start> FALSE POSITIVE -- needed for ncurses <justification end> */
|
||||
wrefresh(W); /* Refresh ncurses window */
|
||||
|
||||
} while (true); /* Repeat until user selects q to quit */
|
||||
|
||||
/* free the memory */
|
||||
for(y = (unsigned)0; y < row; y++)
|
||||
{
|
||||
free(maze[y]);
|
||||
maze[y] = NULL;
|
||||
}
|
||||
if(maze != NULL){
|
||||
free(maze);
|
||||
}
|
||||
/*printw("\n");*/
|
||||
/* LDRA_EXCLUDE 170 S <justification start> FALSE POSITIVE -- needed for ncurses <justification end> */
|
||||
/* LDRA_EXCLUDE 496 S <justification start> FALSE POSITIVE -- needed for ncurses <justification end> */
|
||||
/* LDRA_EXCLUDE 41 D <justification start> FALSE POSITIVE -- needed for ncurses <justification end> */
|
||||
endwin();
|
||||
printf("Score = %u\n", score);
|
||||
return 0;
|
||||
}
|
||||
|
@ -0,0 +1,129 @@
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <stdint.h>
|
||||
#include <string.h>
|
||||
#include <ncurses.h>
|
||||
|
||||
|
||||
|
||||
|
||||
int main(int argc, char *argv[])
|
||||
{
|
||||
FILE* mazeFile; /* Maze Input File */
|
||||
unsigned int row = 0; /* Holds number of rows for input from file */
|
||||
unsigned int col = 0; /* Holds number of cols for input from file */
|
||||
char shold[2000]; /* Holds lines for input from file */
|
||||
unsigned int l = 0;
|
||||
unsigned int k = 0;
|
||||
char **maze; /* array for maze */
|
||||
unsigned int c1 = 0;
|
||||
unsigned int c2 = 0;
|
||||
unsigned int i = 0;
|
||||
unsigned int j = 0;
|
||||
unsigned int x = 0;
|
||||
unsigned int y = 0;
|
||||
|
||||
/* check to see if argument */
|
||||
if (argc != 2)
|
||||
{
|
||||
printf("An error has ocurred no input file was given.\n");
|
||||
return 1;
|
||||
}
|
||||
mazeFile = fopen(argv[1], "r");
|
||||
if (mazeFile == NULL)
|
||||
{
|
||||
printf("Error in opening the file.\n");
|
||||
return 1;
|
||||
}
|
||||
fgets(shold, 2, mazeFile);
|
||||
while ( feof(mazeFile) == 0 )
|
||||
{
|
||||
|
||||
switch(shold[0])
|
||||
{
|
||||
case 'M':
|
||||
/* read array values, dynamically allocate array */
|
||||
fscanf(mazeFile, "%3u %3u", &row, &col);
|
||||
maze = (char**)malloc(row * 8);
|
||||
for(i = (unsigned) 0; i < row; i++)
|
||||
{
|
||||
if (maze==NULL)
|
||||
{
|
||||
break;
|
||||
}
|
||||
maze[i] = malloc(col * 8);
|
||||
}
|
||||
for (x = (unsigned) 0; x < row; x++)
|
||||
{
|
||||
for (j = (unsigned) 0; j < col; j++)
|
||||
{
|
||||
fscanf(mazeFile, "%c", &maze[x][j]);
|
||||
if (maze[x][j] == '\n')
|
||||
{
|
||||
fscanf(mazeFile, "%c", &maze[x][j]);
|
||||
}
|
||||
}
|
||||
}
|
||||
break;
|
||||
case 'P':
|
||||
fscanf(mazeFile, "%3u %3u", &k, &l);
|
||||
/* check maze bounds */
|
||||
if(k>= row || l>= col){
|
||||
break;
|
||||
}
|
||||
maze[k][l] = 'P';
|
||||
break;
|
||||
|
||||
case 'Z':
|
||||
fscanf(mazeFile, "%3u %3u", &k, &l);
|
||||
/* check maze bounds */
|
||||
if(k>= row || l>= col){
|
||||
break;
|
||||
}
|
||||
maze[k][l] = 'Z';
|
||||
break;
|
||||
|
||||
case 'T':
|
||||
fscanf(mazeFile, "%3u %3u", &k, &l);
|
||||
/* check maze bounds */
|
||||
if(k>= row || l>= col){
|
||||
break;
|
||||
}
|
||||
maze[k][l] = 'T';
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
};
|
||||
fgets(shold, 2, mazeFile);
|
||||
if (feof(mazeFile) != 0)
|
||||
{
|
||||
break;
|
||||
}
|
||||
}
|
||||
fclose(mazeFile);
|
||||
/* print out maze */
|
||||
for (c1 =(unsigned)0; c1 < row; c1++)
|
||||
{
|
||||
printf ("\n");
|
||||
for (c2=(unsigned)0; c2 < col; c2++)
|
||||
{
|
||||
if (maze==NULL)
|
||||
{
|
||||
break;
|
||||
}
|
||||
printf("%c", maze[c1][c2]);
|
||||
}
|
||||
}
|
||||
|
||||
/* free the memory */
|
||||
for(y = (unsigned)0; y < row; y++)
|
||||
{
|
||||
free(maze[y]);
|
||||
maze[y] = NULL;
|
||||
}
|
||||
if(maze != NULL){
|
||||
free(maze);
|
||||
}
|
||||
printf("\n");
|
||||
return 0;
|
||||
}
|
@ -0,0 +1,256 @@
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <stdint.h>
|
||||
#include <string.h>
|
||||
#include <unistd.h>
|
||||
#include <ncurses.h>
|
||||
|
||||
|
||||
/*nothing*/
|
||||
|
||||
int main(int argc, char *argv[])
|
||||
{
|
||||
FILE* mazeFile; /* Maze Input File */
|
||||
unsigned int row = 0; /* Holds number of rows for input from file */
|
||||
unsigned int col = 0; /* Holds number of cols for input from file */
|
||||
unsigned int pcol = 0;
|
||||
unsigned int prow = 0;
|
||||
char shold[2000]; /* Holds lines for input from file */
|
||||
unsigned int l = 0;
|
||||
unsigned int k = 0;
|
||||
char **maze; /* array for maze */
|
||||
unsigned int c1 = 0;
|
||||
unsigned int c2 = 0;
|
||||
unsigned int i = 0;
|
||||
unsigned int j = 0;
|
||||
unsigned int x = 0;
|
||||
unsigned int y = 0;
|
||||
int deltaRow, deltaCol; /* Change in row-column position based on key input */
|
||||
|
||||
|
||||
WINDOW* W = 0; /* Curses Window handle */
|
||||
int rowLimit,colLimit; /* Num of rows and columns in terminal window */
|
||||
/* Initialize ncurses -- you will want to use the following settings */
|
||||
|
||||
|
||||
|
||||
/* check to see if argument */
|
||||
if (argc != 2)
|
||||
{
|
||||
/*printw("An error has ocurred no input file was given.\n");*/
|
||||
return 1;
|
||||
}
|
||||
mazeFile = fopen(argv[1], "r");
|
||||
if (mazeFile == NULL)
|
||||
{
|
||||
/*printw("Error in opening the file.\n");*/
|
||||
return 1;
|
||||
}
|
||||
fgets(shold, 2, mazeFile);
|
||||
while ( feof(mazeFile) == 0 )
|
||||
{
|
||||
|
||||
|
||||
switch(shold[0])
|
||||
{
|
||||
case 'M':
|
||||
/* read array values, dynamically allocate array */
|
||||
fscanf(mazeFile, "%3u %3u", &row, &col);
|
||||
maze = (char**)malloc(row * 8);
|
||||
for(i = (unsigned) 0; i < row; i++)
|
||||
{
|
||||
if (maze==NULL)
|
||||
{
|
||||
break;
|
||||
}
|
||||
maze[i] = malloc(col * 8);
|
||||
}
|
||||
for (x = (unsigned) 0; x < row; x++)
|
||||
{
|
||||
for (j = (unsigned) 0; j < col; j++)
|
||||
{
|
||||
fscanf(mazeFile, "%c", &maze[x][j]);
|
||||
if (maze[x][j] == '\n')
|
||||
{
|
||||
fscanf(mazeFile, "%c", &maze[x][j]);
|
||||
}
|
||||
}
|
||||
}
|
||||
break;
|
||||
case 'P':
|
||||
fscanf(mazeFile, "%3u %3u", &k, &l);
|
||||
/* check maze bounds */
|
||||
if(k>= row || l>= col){
|
||||
break;
|
||||
}
|
||||
maze[k][l] = 'P';
|
||||
break;
|
||||
|
||||
case 'Z':
|
||||
fscanf(mazeFile, "%3u %3u", &k, &l);
|
||||
/* check maze bounds */
|
||||
if(k>= row || l>= col){
|
||||
break;
|
||||
}
|
||||
maze[k][l] = 'Z';
|
||||
break;
|
||||
|
||||
case 'T':
|
||||
fscanf(mazeFile, "%3u %3u", &k, &l);
|
||||
/* check maze bounds */
|
||||
if(k>= row || l>= col){
|
||||
break;
|
||||
}
|
||||
maze[k][l] = 'T';
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
};
|
||||
fgets(shold, 2, mazeFile);
|
||||
if (feof(mazeFile) != 0)
|
||||
{
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
fclose(mazeFile);
|
||||
W = initscr(); /* Determine terminal type and initialize curses data structures */
|
||||
start_color(); /* Enable use of color with ncurses */
|
||||
cbreak(); /* Disable line buffering and make char inputs immediately accessible to program */
|
||||
noecho(); /* Disable echo printing of chars type by user */
|
||||
nodelay(W, true); /* Make getch( ) a non-blocking call */
|
||||
keypad(W, true); /* Enable keypad and arrow keys */
|
||||
curs_set(0); /* Set cursor to be invisible - 0 */
|
||||
getmaxyx(W, rowLimit, colLimit); /* Query terminal dimensions */
|
||||
|
||||
/* Define color pallete foreground-background color pairs */
|
||||
/* PairID#, Foreground Color, Background Color */
|
||||
/* Foreground color == Background color ==> solid color block */
|
||||
init_pair(1, COLOR_BLACK, COLOR_BLACK); /* Black text on Black background */
|
||||
init_pair(2, COLOR_GREEN, COLOR_GREEN); /* Red text on Black background */
|
||||
init_pair(3, COLOR_WHITE, COLOR_WHITE); /* White text on Blue background */
|
||||
init_pair(4, COLOR_RED, COLOR_RED); /* Yellow text on Yellow background */
|
||||
init_pair(5, COLOR_YELLOW, COLOR_YELLOW); /* Yellow text on Yellow background */
|
||||
|
||||
|
||||
for (c1 =(unsigned)0; c1 < row; c1++)
|
||||
{
|
||||
/*printw ("\n");*/
|
||||
for (c2=(unsigned)0; c2 < col; c2++)
|
||||
{
|
||||
if (maze==NULL)
|
||||
{
|
||||
break;
|
||||
}
|
||||
switch(maze[c1][c2])
|
||||
{
|
||||
case '#':
|
||||
/* read array values, dynamically allocate array*/
|
||||
attron(COLOR_PAIR(2));
|
||||
wmove(W, c1,c2);
|
||||
waddch(W,'#');
|
||||
attroff(COLOR_PAIR(2));
|
||||
break;
|
||||
case ' ':
|
||||
wmove(W, c1,c2);
|
||||
waddch(W,' ');
|
||||
break;
|
||||
case 'P':
|
||||
wmove(W, c1,c2);
|
||||
waddch(W,'P');
|
||||
prow=c1;
|
||||
pcol=c2;
|
||||
break;
|
||||
case 'Z':
|
||||
wmove(W, c1,c2);
|
||||
waddch(W,'Z');
|
||||
break;
|
||||
case 'T':
|
||||
wmove(W, c1,c2);
|
||||
waddch(W,'T');
|
||||
break;
|
||||
|
||||
default:
|
||||
break;
|
||||
};
|
||||
/*printw("%c", maze[c1][c2]);*/
|
||||
}
|
||||
}
|
||||
|
||||
deltaRow =0;
|
||||
deltaCol =0;
|
||||
|
||||
|
||||
|
||||
do{
|
||||
|
||||
int kb = wgetch(W); /* Grab keypress */
|
||||
|
||||
/* Support player movement via WASD, IJKL, and arrow keys */
|
||||
if (kb == 'a' || kb == 'j' || kb == KEY_LEFT)
|
||||
{
|
||||
/* Move Left */
|
||||
deltaRow =0;
|
||||
deltaCol =-1;
|
||||
}
|
||||
else if (kb == 'd' || kb == 'l' || kb == KEY_RIGHT)
|
||||
{
|
||||
/* Move Right */
|
||||
deltaRow =0;
|
||||
deltaCol =1;
|
||||
}
|
||||
else if (kb == 'w' || kb == 'i' || kb == KEY_UP)
|
||||
{
|
||||
/* Move Up */
|
||||
deltaRow =-1;
|
||||
deltaCol =0;
|
||||
}
|
||||
else if (kb == 's' || kb == 'k' || kb == KEY_DOWN)
|
||||
{
|
||||
/* Move Down */
|
||||
deltaRow =1;
|
||||
deltaCol =0;
|
||||
}
|
||||
else if (kb == 'q')
|
||||
{
|
||||
/* q to exit player movement loop */
|
||||
break;
|
||||
};
|
||||
|
||||
|
||||
attron(COLOR_PAIR(1));
|
||||
wmove(W,prow,pcol);
|
||||
waddch(W, 'P');
|
||||
attroff(COLOR_PAIR(1));
|
||||
|
||||
prow = prow + deltaRow;
|
||||
pcol = pcol + deltaCol;
|
||||
|
||||
attron(COLOR_PAIR(3));
|
||||
wmove(W,prow,pcol);
|
||||
waddch(W, 'P');
|
||||
attroff(COLOR_PAIR(3));
|
||||
|
||||
deltaRow =0;
|
||||
deltaCol =0;
|
||||
|
||||
wrefresh(W); /* Refresh ncurses window */
|
||||
|
||||
} while (true); /* Repeat until user selects q to quit */
|
||||
|
||||
|
||||
/* free the memory */
|
||||
for(y = (unsigned)0; y < row; y++)
|
||||
{
|
||||
free(maze[y]);
|
||||
maze[y] = NULL;
|
||||
}
|
||||
if(maze != NULL){
|
||||
free(maze);
|
||||
}
|
||||
/*printw("\n");*/
|
||||
endwin();
|
||||
return 0;
|
||||
}
|
||||
|
@ -0,0 +1,21 @@
|
||||
M 14 21
|
||||
#####################
|
||||
# # # #
|
||||
# # # #
|
||||
# # # #
|
||||
# ####### #######
|
||||
# #
|
||||
# #
|
||||
# ########### #
|
||||
# # #
|
||||
# # #
|
||||
# ##### #
|
||||
# #
|
||||
# #
|
||||
#####################
|
||||
T 8 15
|
||||
Z 9 10
|
||||
Z 6 11
|
||||
T 2 8
|
||||
P 2 10
|
||||
T 2 12
|
@ -0,0 +1,59 @@
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <stdint.h>
|
||||
#include <string.h>
|
||||
|
||||
void readMaze(char *arr);
|
||||
|
||||
int main(int argc, char *argv[])
|
||||
{
|
||||
printf("Hello, World!\n");
|
||||
FILE* mazeFile; /* Maze Input File */
|
||||
char c; /* Holds character for line input from file */
|
||||
int row; /* Holds number of rows for input from file */
|
||||
int col; /* Holds number of cols for input from file */
|
||||
char shold[2000]; /* Holds lines for input from file */
|
||||
if (argc != 2)
|
||||
{
|
||||
printf("An error has ocurred no input file was given.\n");
|
||||
return 1;
|
||||
}
|
||||
mazeFile = fopen(argv[1], "r");
|
||||
if (mazeFile == NULL)
|
||||
{
|
||||
printf("Error in opening the file.\n");
|
||||
return 1;
|
||||
}
|
||||
fgets(shold, 2, mazeFile);
|
||||
while ( !feof(mazeFile) )
|
||||
{
|
||||
switch(shold[0])
|
||||
{
|
||||
case 'M':
|
||||
printf ("Maze encountered\n");
|
||||
fscanf(mazeFile, "%d %d", &row, &col);
|
||||
printf("%d\n", row);
|
||||
// possible dynamic allocation of 2D array
|
||||
char* maze = malloc((row * col) * sizeof(char));
|
||||
readMaze(maze);
|
||||
break;
|
||||
default:
|
||||
//printf ("Basic error\n");
|
||||
break;
|
||||
};
|
||||
fgets(shold, 2, mazeFile);
|
||||
if (feof(mazeFile))
|
||||
{
|
||||
break;
|
||||
}
|
||||
}
|
||||
fclose(mazeFile);
|
||||
//printf ("So this line won't print without an argument.\n");
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
void readMaze(char *arr)
|
||||
{
|
||||
|
||||
}
|
@ -0,0 +1,123 @@
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <stdint.h>
|
||||
#include <string.h>
|
||||
|
||||
|
||||
|
||||
|
||||
int main(int argc, char *argv[])
|
||||
{
|
||||
FILE* mazeFile; /* Maze Input File */
|
||||
char chold = NULL; /* Holds character for line input from file */
|
||||
unsigned int row = 0; /* Holds number of rows for input from file */
|
||||
unsigned int col = 0; /* Holds number of cols for input from file */
|
||||
char shold[2000]; /* Holds lines for input from file */
|
||||
unsigned int l = 0;
|
||||
unsigned int k = 0;
|
||||
char **maze; // global array for maze
|
||||
unsigned int c1 = 0;
|
||||
unsigned int c2 = 0;
|
||||
|
||||
// check to see if
|
||||
if (argc != 2)
|
||||
{
|
||||
printf("An error has ocurred no input file was given.\n");
|
||||
return 1;
|
||||
}
|
||||
mazeFile = fopen(argv[1], "r");
|
||||
if (mazeFile == NULL)
|
||||
{
|
||||
printf("Error in opening the file.\n");
|
||||
return 1;
|
||||
}
|
||||
fgets(shold, 2, mazeFile);
|
||||
while ( feof(mazeFile) == 0 )
|
||||
{
|
||||
|
||||
switch(shold[0])
|
||||
{
|
||||
case 'M':
|
||||
fscanf(mazeFile, "%3d %3d", &row, &col);
|
||||
// dynamic allocation of array for maze
|
||||
unsigned int i = 0, j = 0;
|
||||
maze = malloc(row * sizeof(unsigned int *));
|
||||
for(i = (unsigned) 0; i < row; i++)
|
||||
{
|
||||
if (maze==NULL)
|
||||
{
|
||||
break;
|
||||
}
|
||||
maze[i] = malloc(col * sizeof(unsigned int *));
|
||||
}
|
||||
for (i = (unsigned) 0; i < row; i++)
|
||||
{
|
||||
for (j = (unsigned) 0; j < col; j++)
|
||||
{
|
||||
fscanf(mazeFile, "%c", &maze[i][j]);
|
||||
if (maze[i][j] == '\n')
|
||||
{
|
||||
fscanf(mazeFile, "%c", &maze[i][j]);
|
||||
}
|
||||
}
|
||||
}
|
||||
break;
|
||||
case 'P':
|
||||
fscanf(mazeFile, "%3d %3d", &k, &l);
|
||||
if(k>= row || l>= col){
|
||||
break;
|
||||
}
|
||||
maze[k][l] = 'P';
|
||||
break;
|
||||
|
||||
case 'Z':
|
||||
fscanf(mazeFile, "%3d %3d", &k, &l);
|
||||
if(k>= row || l>= col){
|
||||
break;
|
||||
}
|
||||
maze[k][l] = 'Z';
|
||||
break;
|
||||
|
||||
case 'T':
|
||||
fscanf(mazeFile, "%3d %3d", &k, &l);
|
||||
if(k>= row || l>= col){
|
||||
break;
|
||||
}
|
||||
maze[k][l] = 'T';
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
};
|
||||
fgets(shold, 2, mazeFile);
|
||||
if (feof(mazeFile) != 0)
|
||||
{
|
||||
break;
|
||||
}
|
||||
}
|
||||
fclose(mazeFile);
|
||||
for (c1 =(unsigned)0; c1 < row; c1++)
|
||||
{
|
||||
printf ("\n");
|
||||
for (c2=(unsigned)0; c2 < col; c2++)
|
||||
{
|
||||
if (maze==NULL)
|
||||
{
|
||||
break;
|
||||
}
|
||||
printf("%c", maze[c1][c2]);
|
||||
}
|
||||
}
|
||||
|
||||
//free the memory
|
||||
unsigned int i = 0;
|
||||
for(i = (unsigned)0; i < row; i++)
|
||||
{
|
||||
free(maze[i]);
|
||||
}
|
||||
if(maze != NULL){
|
||||
free(maze);
|
||||
}
|
||||
printf("\n");
|
||||
|
||||
return 0;
|
||||
}
|
@ -0,0 +1,290 @@
|
||||
#include <ncurses.h>
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <stdint.h>
|
||||
#include <string.h>
|
||||
#include <unistd.h>
|
||||
|
||||
WINDOW *initscr(void);
|
||||
int start_color(void);
|
||||
int cbreak(void);
|
||||
int noecho(void);
|
||||
int nodelay(WINDOW *win, bool bf);
|
||||
int keypad(WINDOW *win, bool bf);
|
||||
int curs_set(int visibility);
|
||||
/*void getmaxyx(WINDOW *win, int y, int x); */
|
||||
int init_pair(short pair, short f, short b);
|
||||
/*int attron(int attrs);
|
||||
int COLOR_PAIR(int n);
|
||||
int wmove(WINDOW *win, int y, int x);
|
||||
nothing*/
|
||||
|
||||
int main(int argc, char *argv[])
|
||||
{
|
||||
FILE* mazeFile; /* Maze Input File */
|
||||
unsigned int row = 0; /* Holds number of rows for input from file */
|
||||
unsigned int col = 0; /* Holds number of cols for input from file */
|
||||
unsigned int pcol = 0;
|
||||
unsigned int prow = 0;
|
||||
char shold[2000]; /* Holds lines for input from file */
|
||||
unsigned int l = 0;
|
||||
unsigned int k = 0;
|
||||
char **maze; /* array for maze */
|
||||
unsigned int c1 = 0;
|
||||
unsigned int c2 = 0;
|
||||
unsigned int i = 0;
|
||||
unsigned int j = 0;
|
||||
unsigned int x = 0;
|
||||
unsigned int y = 0;
|
||||
int deltaRow, deltaCol; /* Change in row-column position based on key input */
|
||||
unsigned int score = 0;
|
||||
|
||||
|
||||
WINDOW* W = 0; /* Curses Window handle */
|
||||
int rowLimit,colLimit; /* Num of rows and columns in terminal window */
|
||||
/* Initialize ncurses -- you will want to use the following settings */
|
||||
|
||||
|
||||
|
||||
/* check to see if argument */
|
||||
if (argc != 2)
|
||||
{
|
||||
/*printw("An error has ocurred no input file was given.\n");*/
|
||||
return 1;
|
||||
}
|
||||
mazeFile = fopen(argv[1], "r");
|
||||
if (mazeFile == NULL)
|
||||
{
|
||||
/*printw("Error in opening the file.\n");*/
|
||||
return 1;
|
||||
}
|
||||
fgets(shold, 2, mazeFile);
|
||||
while ( feof(mazeFile) == 0 )
|
||||
{
|
||||
|
||||
|
||||
switch(shold[0])
|
||||
{
|
||||
case 'M':
|
||||
/* read array values, dynamically allocate array */
|
||||
fscanf(mazeFile, "%3u %3u", &row, &col);
|
||||
maze = (char**)malloc(row * 8);
|
||||
for(i = (unsigned) 0; i < row; i++)
|
||||
{
|
||||
if (maze==NULL)
|
||||
{
|
||||
break;
|
||||
}
|
||||
maze[i] = malloc(col * 8);
|
||||
}
|
||||
for (x = (unsigned) 0; x < row; x++)
|
||||
{
|
||||
for (j = (unsigned) 0; j < col; j++)
|
||||
{
|
||||
fscanf(mazeFile, "%c", &maze[x][j]);
|
||||
if (maze[x][j] == '\n')
|
||||
{
|
||||
fscanf(mazeFile, "%c", &maze[x][j]);
|
||||
}
|
||||
}
|
||||
}
|
||||
break;
|
||||
case 'P':
|
||||
fscanf(mazeFile, "%3u %3u", &k, &l);
|
||||
/* check maze bounds */
|
||||
if(k>= row || l>= col){
|
||||
break;
|
||||
}
|
||||
maze[k][l] = 'P';
|
||||
break;
|
||||
|
||||
case 'Z':
|
||||
fscanf(mazeFile, "%3u %3u", &k, &l);
|
||||
/* check maze bounds */
|
||||
if(k>= row || l>= col){
|
||||
break;
|
||||
}
|
||||
maze[k][l] = 'Z';
|
||||
break;
|
||||
|
||||
case 'T':
|
||||
fscanf(mazeFile, "%3u %3u", &k, &l);
|
||||
/* check maze bounds */
|
||||
if(k>= row || l>= col){
|
||||
break;
|
||||
}
|
||||
maze[k][l] = 'T';
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
};
|
||||
fgets(shold, 2, mazeFile);
|
||||
if (feof(mazeFile) != 0)
|
||||
{
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
fclose(mazeFile);
|
||||
W = initscr(); /* Determine terminal type and initialize curses data structures */
|
||||
start_color(); /* Enable use of color with ncurses */
|
||||
cbreak(); /* Disable line buffering and make char inputs immediately accessible to program */
|
||||
noecho(); /* Disable echo printing of chars type by user */
|
||||
nodelay(W, true); /* Make getch( ) a non-blocking call */
|
||||
keypad(W, true); /* Enable keypad and arrow keys */
|
||||
curs_set(0); /* Set cursor to be invisible - 0 */
|
||||
getmaxyx(W, rowLimit, colLimit); /* Query terminal dimensions */
|
||||
|
||||
/* Define color pallete foreground-background color pairs */
|
||||
/* PairID#, Foreground Color, Background Color */
|
||||
/* Foreground color == Background color ==> solid color block */
|
||||
init_pair(1, COLOR_BLACK, COLOR_BLACK); /* Black text on Black background */
|
||||
init_pair(2, COLOR_GREEN, COLOR_GREEN); /* Red text on Black background */
|
||||
init_pair(3, COLOR_WHITE, COLOR_WHITE); /* White text on Blue background */
|
||||
init_pair(4, COLOR_RED, COLOR_RED); /* Yellow text on Yellow background */
|
||||
init_pair(5, COLOR_YELLOW, COLOR_YELLOW); /* Yellow text on Yellow background */
|
||||
|
||||
|
||||
for (c1 =(unsigned)0; c1 < row; c1++)
|
||||
{
|
||||
/*printw ("\n");*/
|
||||
for (c2=(unsigned)0; c2 < col; c2++)
|
||||
{
|
||||
if (maze==NULL)
|
||||
{
|
||||
break;
|
||||
}
|
||||
switch(maze[c1][c2])
|
||||
{
|
||||
case '#':
|
||||
/* read array values, dynamically allocate array*/
|
||||
attron(COLOR_PAIR(2));
|
||||
wmove(W, c1,c2);
|
||||
waddch(W,'#');
|
||||
attroff(COLOR_PAIR(2));
|
||||
break;
|
||||
case ' ':
|
||||
wmove(W, c1,c2);
|
||||
waddch(W,' ');
|
||||
break;
|
||||
case 'P':
|
||||
wmove(W, c1,c2);
|
||||
waddch(W,'P');
|
||||
prow=c1;
|
||||
pcol=c2;
|
||||
break;
|
||||
case 'Z':
|
||||
attron(COLOR_PAIR(4));
|
||||
wmove(W, c1,c2);
|
||||
waddch(W,'Z');
|
||||
attroff(COLOR_PAIR(4));
|
||||
break;
|
||||
case 'T':
|
||||
attron(COLOR_PAIR(5));
|
||||
wmove(W, c1,c2);
|
||||
waddch(W,'T');
|
||||
attroff(COLOR_PAIR(5));
|
||||
break;
|
||||
|
||||
default:
|
||||
break;
|
||||
};
|
||||
/*printw("%c", maze[c1][c2]);*/
|
||||
}
|
||||
}
|
||||
|
||||
deltaRow =0;
|
||||
deltaCol =0;
|
||||
|
||||
|
||||
|
||||
do{
|
||||
|
||||
int kb = wgetch(W); /* Grab keypress */
|
||||
|
||||
/* Support player movement via WASD, IJKL, and arrow keys */
|
||||
if (kb == (int)'a' || kb == (int)'j' || kb == KEY_LEFT)
|
||||
{
|
||||
/* Move Left */
|
||||
deltaRow =0;
|
||||
deltaCol =-1;
|
||||
}
|
||||
else if (kb == (int)'d' || kb == (int)'l' || kb == KEY_RIGHT)
|
||||
{
|
||||
/* Move Right */
|
||||
deltaRow =0;
|
||||
deltaCol =1;
|
||||
}
|
||||
else if (kb == (int)'w' || kb == (int)'i' || kb == KEY_UP)
|
||||
{
|
||||
/* Move Up */
|
||||
deltaRow =-1;
|
||||
deltaCol =0;
|
||||
}
|
||||
else if (kb == (int)'s' || kb == (int)'k' || kb == KEY_DOWN)
|
||||
{
|
||||
/* Move Down */
|
||||
deltaRow =1;
|
||||
deltaCol =0;
|
||||
}
|
||||
else if (kb == (int)'q')
|
||||
{
|
||||
/* q to exit player movement loop */
|
||||
break;
|
||||
};
|
||||
|
||||
attron(COLOR_PAIR(1));
|
||||
wmove(W,prow,pcol);
|
||||
waddch(W, 'P');
|
||||
attroff(COLOR_PAIR(1));
|
||||
|
||||
if((((int)prow + deltaRow) > row) || (((int)pcol + deltaCol) > col)){
|
||||
deltaRow = 0;
|
||||
deltaCol = 0;
|
||||
}
|
||||
if(maze[((int)prow + deltaRow)][((int)pcol + deltaCol)] == '#'){
|
||||
prow = prow;
|
||||
pcol = pcol;
|
||||
}
|
||||
else{
|
||||
prow = prow + deltaRow;
|
||||
pcol = pcol + deltaCol;
|
||||
}
|
||||
if(maze[prow][pcol] == 'T')
|
||||
{
|
||||
maze[prow][pcol] = ' ';
|
||||
score++;
|
||||
}
|
||||
if(maze[prow][pcol] == 'Z')
|
||||
{
|
||||
break;
|
||||
}
|
||||
|
||||
attron(COLOR_PAIR(3));
|
||||
wmove(W,prow,pcol);
|
||||
waddch(W, 'P');
|
||||
attroff(COLOR_PAIR(3));
|
||||
|
||||
deltaRow =0;
|
||||
deltaCol =0;
|
||||
|
||||
wrefresh(W); /* Refresh ncurses window */
|
||||
|
||||
} while (true); /* Repeat until user selects q to quit */
|
||||
|
||||
/* free the memory */
|
||||
for(y = (unsigned)0; y < row; y++)
|
||||
{
|
||||
free(maze[y]);
|
||||
maze[y] = NULL;
|
||||
}
|
||||
if(maze != NULL){
|
||||
free(maze);
|
||||
}
|
||||
/*printw("\n");*/
|
||||
endwin();
|
||||
printf("Score = %u\n", score);
|
||||
return 0;
|
||||
}
|
||||
|
@ -0,0 +1,79 @@
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <stdint.h>
|
||||
#include <string.h>
|
||||
|
||||
|
||||
// read maze file in to the array
|
||||
void readMaze(char *arr, int r, int c);
|
||||
|
||||
int main(int argc, char *argv[])
|
||||
{
|
||||
printf("Hello, World!\n");
|
||||
FILE* mazeFile; /* Maze Input File */
|
||||
char c; /* Holds character for line input from file */
|
||||
int row; /* Holds number of rows for input from file */
|
||||
int col; /* Holds number of cols for input from file */
|
||||
char shold[2000]; /* Holds lines for input from file */
|
||||
if (argc != 2)
|
||||
{
|
||||
printf("An error has ocurred no input file was given.\n");
|
||||
return 1;
|
||||
}
|
||||
mazeFile = fopen(argv[1], "r");
|
||||
if (mazeFile == NULL)
|
||||
{
|
||||
printf("Error in opening the file.\n");
|
||||
return 1;
|
||||
}
|
||||
fgets(shold, 2, mazeFile);
|
||||
while ( !feof(mazeFile) )
|
||||
{
|
||||
switch(shold[0])
|
||||
{
|
||||
case 'M':
|
||||
printf ("Maze encountered\n");
|
||||
fscanf(mazeFile, "%d %d", &row, &col);
|
||||
printf("%d\n", row);
|
||||
// possible dynamic allocation of 2D array
|
||||
int i = 0, j = 0;
|
||||
char **maze = malloc(row * sizeof(char *));
|
||||
for(i = 0; i < row; i++)
|
||||
maze[i] = malloc(col * sizeof(char));
|
||||
for (i = 0; i < row; i++)
|
||||
{
|
||||
for (j = 0; j < col; j++)
|
||||
{
|
||||
fscanf(mazeFile, "%c", &maze[i][j]);
|
||||
printf("%c", maze[i][j]);
|
||||
}
|
||||
|
||||
}
|
||||
printf("%c", maze[0 * col + 0]);
|
||||
for(i = 0; i < row; i++)
|
||||
free(maze[i]);
|
||||
free(maze);
|
||||
//int (*maze)[col] = malloc(sizeof(int[row][col]));
|
||||
//readMaze(maze, row, col);
|
||||
break;
|
||||
default:
|
||||
//printf ("Basic error\n");
|
||||
break;
|
||||
};
|
||||
fgets(shold, 2, mazeFile);
|
||||
if (feof(mazeFile))
|
||||
{
|
||||
break;
|
||||
}
|
||||
}
|
||||
fclose(mazeFile);
|
||||
//printf ("So this line won't print without an argument.\n");
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
void readMaze(char *arr, int row, int col)
|
||||
{
|
||||
|
||||
|
||||
}
|
@ -0,0 +1,146 @@
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <stdint.h>
|
||||
#include <string.h>
|
||||
|
||||
void readPlayer(char *arr);
|
||||
void readZombie(char *arr);
|
||||
void readTreasure(char *arr);
|
||||
|
||||
// read maze file in to the array
|
||||
void readMaze(char *arr, int r, int c);
|
||||
|
||||
int main(int argc, char *argv[])
|
||||
{
|
||||
printf("Hello, World!\n");
|
||||
FILE* mazeFile; /* Maze Input File */
|
||||
char c; /* Holds character for line input from file */
|
||||
int row; /* Holds number of rows for input from file */
|
||||
int col; /* Holds number of cols for input from file */
|
||||
char shold[2000]; /* Holds lines for input from file */
|
||||
|
||||
if (argc != 2)
|
||||
{
|
||||
printf("An error has ocurred no input file was given.\n");
|
||||
return 1;
|
||||
}
|
||||
mazeFile = fopen(argv[1], "r");
|
||||
if (mazeFile == NULL)
|
||||
{
|
||||
printf("Error in opening the file.\n");
|
||||
return 1;
|
||||
}
|
||||
fgets(shold, 2, mazeFile);
|
||||
while ( !feof(mazeFile) )
|
||||
{
|
||||
|
||||
switch(shold[0])
|
||||
{
|
||||
case 'M':
|
||||
printf ("Maze encountered\n");
|
||||
fscanf(mazeFile, "%d %d", &row, &col);
|
||||
printf("%d\n", row);
|
||||
// possible dynamic allocation of 2D array
|
||||
int i = 0, j = 0;
|
||||
char **maze = malloc(row * sizeof(char *));
|
||||
for(i = 0; i < row; i++)
|
||||
maze[i] = malloc(col * sizeof(char));
|
||||
for (i = 0; i < row; i++)
|
||||
{
|
||||
for (j = 0; j < col; j++)
|
||||
{
|
||||
fscanf(mazeFile, "%c", &maze[i][j]);
|
||||
printf("%c", maze[i][j]);
|
||||
}
|
||||
|
||||
}
|
||||
printf("%c", maze[0 * col + 0]);
|
||||
for(i = 0; i < row; i++)
|
||||
free(maze[i]);
|
||||
free(maze);
|
||||
//int (*maze)[col] = malloc(sizeof(int[row][col]));
|
||||
//readMaze(maze, row, col);
|
||||
break;
|
||||
case 'P':
|
||||
//case for player
|
||||
printf ("Player\n");
|
||||
fscanf(mazeFile, "%d %d", &row, &col);
|
||||
printf("%d\n", row);
|
||||
// possible dynamic allocation of 2D array
|
||||
char* player = malloc((row * col) * sizeof(char));
|
||||
readPlayer(player);
|
||||
break;
|
||||
|
||||
case 'Z':
|
||||
//case for zombie
|
||||
printf ("Zombie\n");
|
||||
fscanf(mazeFile, "%d %d", &row, &col);
|
||||
printf("%d\n", row);
|
||||
// possible dynamic allocation of 2D array
|
||||
char* zombie = malloc((row * col) * sizeof(char));
|
||||
readZombie(zombie);
|
||||
break;
|
||||
|
||||
case 'T':
|
||||
//case for treasure
|
||||
printf ("Treasure\n");
|
||||
fscanf(mazeFile, "%d %d", &row, &col);
|
||||
printf("%d\n", row);
|
||||
// possible dynamic allocation of 2D array
|
||||
char* treasure = malloc((row * col) * sizeof(char));
|
||||
readTreasure(treasure);
|
||||
break;
|
||||
default:
|
||||
//printf ("Basic error\n");
|
||||
break;
|
||||
};
|
||||
|
||||
for(int i=0;i<row;i++)
|
||||
{
|
||||
for(int j=0;j<col;j++)
|
||||
{
|
||||
if(fscanf == #) //if file has #
|
||||
{
|
||||
maze[i][j] = #;
|
||||
}
|
||||
else
|
||||
{
|
||||
maze[i][j] = " ";
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fgets(shold, 2, mazeFile);
|
||||
if (feof(mazeFile))
|
||||
{
|
||||
break;
|
||||
}
|
||||
}
|
||||
fclose(mazeFile);
|
||||
//printf ("So this line won't print without an argument.\n");
|
||||
|
||||
//free the memory
|
||||
free(maze);
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
void readMaze(char *arr, int row, int col)
|
||||
{
|
||||
|
||||
|
||||
}
|
||||
|
||||
void readPlayer(char *arr)
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
void readZombie(char *arr)
|
||||
{
|
||||
|
||||
}
|
||||
void readTreasure(char *arr)
|
||||
{
|
||||
|
||||
}
|
@ -0,0 +1,255 @@
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <stdint.h>
|
||||
#include <string.h>
|
||||
#include <unistd.h>
|
||||
#include <ncurses.h>
|
||||
|
||||
|
||||
/*nothing*/
|
||||
|
||||
int main(int argc, char *argv[])
|
||||
{
|
||||
FILE* mazeFile; /* Maze Input File */
|
||||
unsigned int row = 0; /* Holds number of rows for input from file */
|
||||
unsigned int col = 0; /* Holds number of cols for input from file */
|
||||
unsigned int pcol = 0;
|
||||
unsigned int prow = 0;
|
||||
char shold[2000]; /* Holds lines for input from file */
|
||||
unsigned int l = 0;
|
||||
unsigned int k = 0;
|
||||
char **maze; /* array for maze */
|
||||
unsigned int c1 = 0;
|
||||
unsigned int c2 = 0;
|
||||
unsigned int i = 0;
|
||||
unsigned int j = 0;
|
||||
unsigned int x = 0;
|
||||
unsigned int y = 0;
|
||||
int deltaRow, deltaCol; /* Change in row-column position based on key input */
|
||||
|
||||
|
||||
WINDOW* W = 0; /* Curses Window handle */
|
||||
int rowLimit,colLimit; /* Num of rows and columns in terminal window */
|
||||
/* Initialize ncurses -- you will want to use the following settings */
|
||||
|
||||
|
||||
|
||||
/* check to see if argument */
|
||||
if (argc != 2)
|
||||
{
|
||||
/*printw("An error has ocurred no input file was given.\n");*/
|
||||
return 1;
|
||||
}
|
||||
mazeFile = fopen(argv[1], "r");
|
||||
if (mazeFile == NULL)
|
||||
{
|
||||
/*printw("Error in opening the file.\n");*/
|
||||
return 1;
|
||||
}
|
||||
fgets(shold, 2, mazeFile);
|
||||
while ( feof(mazeFile) == 0 )
|
||||
{
|
||||
|
||||
|
||||
switch(shold[0])
|
||||
{
|
||||
case 'M':
|
||||
/* read array values, dynamically allocate array */
|
||||
fscanf(mazeFile, "%3u %3u", &row, &col);
|
||||
maze = (char**)malloc(row * 8);
|
||||
for(i = (unsigned) 0; i < row; i++)
|
||||
{
|
||||
if (maze==NULL)
|
||||
{
|
||||
break;
|
||||
}
|
||||
maze[i] = malloc(col * 8);
|
||||
}
|
||||
for (x = (unsigned) 0; x < row; x++)
|
||||
{
|
||||
for (j = (unsigned) 0; j < col; j++)
|
||||
{
|
||||
fscanf(mazeFile, "%c", &maze[x][j]);
|
||||
if (maze[x][j] == '\n')
|
||||
{
|
||||
fscanf(mazeFile, "%c", &maze[x][j]);
|
||||
}
|
||||
}
|
||||
}
|
||||
break;
|
||||
case 'P':
|
||||
fscanf(mazeFile, "%3u %3u", &k, &l);
|
||||
/* check maze bounds */
|
||||
if(k>= row || l>= col){
|
||||
break;
|
||||
}
|
||||
maze[k][l] = 'P';
|
||||
break;
|
||||
|
||||
case 'Z':
|
||||
fscanf(mazeFile, "%3u %3u", &k, &l);
|
||||
/* check maze bounds */
|
||||
if(k>= row || l>= col){
|
||||
break;
|
||||
}
|
||||
maze[k][l] = 'Z';
|
||||
break;
|
||||
|
||||
case 'T':
|
||||
fscanf(mazeFile, "%3u %3u", &k, &l);
|
||||
/* check maze bounds */
|
||||
if(k>= row || l>= col){
|
||||
break;
|
||||
}
|
||||
maze[k][l] = 'T';
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
};
|
||||
fgets(shold, 2, mazeFile);
|
||||
if (feof(mazeFile) != 0)
|
||||
{
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
fclose(mazeFile);
|
||||
W = initscr(); /* Determine terminal type and initialize curses data structures */
|
||||
start_color(); /* Enable use of color with ncurses */
|
||||
cbreak(); /* Disable line buffering and make char inputs immediately accessible to program */
|
||||
noecho(); /* Disable echo printing of chars type by user */
|
||||
nodelay(W, true); /* Make getch( ) a non-blocking call */
|
||||
keypad(W, true); /* Enable keypad and arrow keys */
|
||||
curs_set(0); /* Set cursor to be invisible - 0 */
|
||||
getmaxyx(W, rowLimit, colLimit); /* Query terminal dimensions */
|
||||
|
||||
/* Define color pallete foreground-background color pairs */
|
||||
/* PairID#, Foreground Color, Background Color */
|
||||
/* Foreground color == Background color ==> solid color block */
|
||||
init_pair(1, COLOR_BLACK, COLOR_BLACK); /* Black text on Black background */
|
||||
init_pair(2, COLOR_RED, COLOR_BLACK); /* Red text on Black background */
|
||||
init_pair(3, COLOR_WHITE, COLOR_BLUE); /* White text on Blue background */
|
||||
init_pair(4, COLOR_YELLOW, COLOR_YELLOW); /* Yellow text on Yellow background */
|
||||
|
||||
|
||||
for (c1 =(unsigned)0; c1 < row; c1++)
|
||||
{
|
||||
/*printw ("\n");*/
|
||||
for (c2=(unsigned)0; c2 < col; c2++)
|
||||
{
|
||||
if (maze==NULL)
|
||||
{
|
||||
break;
|
||||
}
|
||||
switch(maze[c1][c2])
|
||||
{
|
||||
case '#':
|
||||
/* read array values, dynamically allocate array*/
|
||||
wmove(W, c1,c2);
|
||||
waddch(W,'#');
|
||||
break;
|
||||
case ' ':
|
||||
wmove(W, c1,c2);
|
||||
waddch(W,' ');
|
||||
break;
|
||||
case 'P':
|
||||
wmove(W, c1,c2);
|
||||
waddch(W,'P');
|
||||
prow=c1;
|
||||
pcol=c2;
|
||||
break;
|
||||
case 'Z':
|
||||
wmove(W, c1,c2);
|
||||
waddch(W,'Z');
|
||||
break;
|
||||
case 'T':
|
||||
wmove(W, c1,c2);
|
||||
waddch(W,'T');
|
||||
break;
|
||||
|
||||
default:
|
||||
break;
|
||||
};
|
||||
/*printw("%c", maze[c1][c2]);*/
|
||||
}
|
||||
}
|
||||
|
||||
prow = rowLimit/4;
|
||||
pcol = colLimit/4;
|
||||
deltaRow =0;
|
||||
deltaCol =0;
|
||||
|
||||
|
||||
|
||||
do{
|
||||
|
||||
int kb = wgetch(W); /* Grab keypress */
|
||||
|
||||
/* Support player movement via WASD, IJKL, and arrow keys */
|
||||
if (kb == 'a' || kb == 'j' || kb == KEY_LEFT)
|
||||
{
|
||||
/* Move Left */
|
||||
deltaRow =0;
|
||||
deltaCol =-1;
|
||||
}
|
||||
else if (kb == 'd' || kb == 'l' || kb == KEY_RIGHT)
|
||||
{
|
||||
/* Move Right */
|
||||
deltaRow =0;
|
||||
deltaCol =1;
|
||||
}
|
||||
else if (kb == 'w' || kb == 'i' || kb == KEY_UP)
|
||||
{
|
||||
/* Move Up */
|
||||
deltaRow =-1;
|
||||
deltaCol =0;
|
||||
}
|
||||
else if (kb == 's' || kb == 'k' || kb == KEY_DOWN)
|
||||
{
|
||||
/* Move Down */
|
||||
deltaRow =1;
|
||||
deltaCol =0;
|
||||
}
|
||||
else if (kb == 'q')
|
||||
{
|
||||
/* q to exit player movement loop */
|
||||
break;
|
||||
};
|
||||
|
||||
|
||||
attron(COLOR_PAIR(1));
|
||||
wmove(W,prow,pcol);
|
||||
waddch(W, 'P');
|
||||
attroff(COLOR_PAIR(1));
|
||||
|
||||
prow = prow + deltaRow;
|
||||
pcol = pcol + deltaCol;
|
||||
|
||||
attron(COLOR_PAIR(1));
|
||||
wmove(W,prow,pcol);
|
||||
waddch(W, 'P');
|
||||
attroff(COLOR_PAIR(1));
|
||||
|
||||
deltaRow =0;
|
||||
deltaCol =0;
|
||||
|
||||
wrefresh(W); /* Refresh ncurses window */
|
||||
|
||||
} while (true); /* Repeat until user selects q to quit */
|
||||
|
||||
|
||||
/* free the memory */
|
||||
for(y = (unsigned)0; y < row; y++)
|
||||
{
|
||||
free(maze[y]);
|
||||
maze[y] = NULL;
|
||||
}
|
||||
if(maze != NULL){
|
||||
free(maze);
|
||||
}
|
||||
/*printw("\n");*/
|
||||
endwin();
|
||||
return 0;
|
||||
}
|
||||
|
@ -0,0 +1,110 @@
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <stdint.h>
|
||||
#include <string.h>
|
||||
|
||||
|
||||
// global array for maze
|
||||
char **maze;
|
||||
|
||||
|
||||
int main(int argc, char *argv[])
|
||||
{
|
||||
FILE* mazeFile; /* Maze Input File */
|
||||
char chold; /* Holds character for line input from file */
|
||||
int row = 0; /* Holds number of rows for input from file */
|
||||
int col = 0; /* Holds number of cols for input from file */
|
||||
char shold[2000]; /* Holds lines for input from file */
|
||||
int l = 0;
|
||||
int k = 0;
|
||||
int c1 = 0;
|
||||
int c2 = 0;
|
||||
|
||||
// check to see if
|
||||
if (argc != 2)
|
||||
{
|
||||
printf("An error has ocurred no input file was given.\n");
|
||||
return 1;
|
||||
}
|
||||
mazeFile = fopen(argv[1], "r");
|
||||
if (mazeFile == NULL)
|
||||
{
|
||||
printf("Error in opening the file.\n");
|
||||
return 1;
|
||||
}
|
||||
fgets(shold, 2, mazeFile);
|
||||
while ( !feof(mazeFile) )
|
||||
{
|
||||
|
||||
switch(shold[0])
|
||||
{
|
||||
case 'M':
|
||||
fscanf(mazeFile, "%d %d", &row, &col);
|
||||
// possible dynamic allocation of 2D array
|
||||
// dynamic allocation of array for maze
|
||||
int i = 0, j = 0;
|
||||
maze = malloc(row * sizeof(char *));
|
||||
for(i = 0; i < row; i++)
|
||||
{
|
||||
maze[i] = malloc(col * sizeof(char));
|
||||
}
|
||||
for (i = 0; i < row; i++)
|
||||
{
|
||||
for (j = 0; j < col; j++)
|
||||
{
|
||||
fscanf(mazeFile, "%c", &maze[i][j]);
|
||||
if (maze[i][j] == '\n')
|
||||
{
|
||||
fscanf(mazeFile, "%c", &maze[i][j]);
|
||||
}
|
||||
}
|
||||
}
|
||||
break;
|
||||
case 'P':
|
||||
fscanf(mazeFile, "%d %d", &k, &l);
|
||||
maze[k][l] = 'P';
|
||||
break;
|
||||
|
||||
case 'Z':
|
||||
fscanf(mazeFile, "%d %d", &k, &l);
|
||||
maze[k][l] = 'Z';
|
||||
printf("%c" , maze[k][l]);
|
||||
break;
|
||||
|
||||
case 'T':
|
||||
fscanf(mazeFile, "%d %d", &k, &l);
|
||||
maze[k][l] = 'T';
|
||||
break;
|
||||
case '\n':
|
||||
printf("\n");
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
};
|
||||
fgets(shold, 2, mazeFile);
|
||||
if (feof(mazeFile))
|
||||
{
|
||||
break;
|
||||
}
|
||||
}
|
||||
fclose(mazeFile);
|
||||
for (c1 =0; c1 < row; c1++)
|
||||
{
|
||||
printf ("\n");
|
||||
for (c2=0; c2 < col; c2++)
|
||||
{
|
||||
printf("%c", maze[c1][c2]);
|
||||
}
|
||||
}
|
||||
|
||||
//free the memory
|
||||
int i = 0;
|
||||
for(i = 0; i < row; i++)
|
||||
{
|
||||
free(maze[i]);
|
||||
}
|
||||
free(maze);
|
||||
printf("\n");
|
||||
|
||||
return 0;
|
||||
}
|
@ -0,0 +1,110 @@
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <stdint.h>
|
||||
#include <string.h>
|
||||
|
||||
|
||||
// global array for maze
|
||||
char **maze;
|
||||
|
||||
|
||||
int main(int argc, char *argv[])
|
||||
{
|
||||
FILE* mazeFile; /* Maze Input File */
|
||||
char chold; /* Holds character for line input from file */
|
||||
int row = 0; /* Holds number of rows for input from file */
|
||||
int col = 0; /* Holds number of cols for input from file */
|
||||
char shold[2000]; /* Holds lines for input from file */
|
||||
int l = 0;
|
||||
int k = 0;
|
||||
int c1 = 0;
|
||||
int c2 = 0;
|
||||
|
||||
// check to see if
|
||||
if (argc != 2)
|
||||
{
|
||||
printf("An error has ocurred no input file was given.\n");
|
||||
return 1;
|
||||
}
|
||||
mazeFile = fopen(argv[1], "r");
|
||||
if (mazeFile == NULL)
|
||||
{
|
||||
printf("Error in opening the file.\n");
|
||||
return 1;
|
||||
}
|
||||
fgets(shold, 2, mazeFile);
|
||||
while ( !feof(mazeFile) )
|
||||
{
|
||||
|
||||
switch(shold[0])
|
||||
{
|
||||
case 'M':
|
||||
fscanf(mazeFile, "%d %d", &row, &col);
|
||||
//
|
||||
// dynamic allocation of array for maze
|
||||
int i = 0, j = 0;
|
||||
maze = calloc(row, sizeof(unsigned int *));
|
||||
for(i = 0; i < row; i++)
|
||||
{
|
||||
maze[i] = calloc(col, sizeof(unsigned int *));
|
||||
}
|
||||
for (i = 0; i < row; i++)
|
||||
{
|
||||
for (j = 0; j < col; j++)
|
||||
{
|
||||
fscanf(mazeFile, "%c", &maze[i][j]);
|
||||
if (maze[i][j] == '\n')
|
||||
{
|
||||
fscanf(mazeFile, "%c", &maze[i][j]);
|
||||
}
|
||||
}
|
||||
}
|
||||
break;
|
||||
case 'P':
|
||||
fscanf(mazeFile, "%d %d", &k, &l);
|
||||
maze[k][l] = 'P';
|
||||
break;
|
||||
|
||||
case 'Z':
|
||||
fscanf(mazeFile, "%d %d", &k, &l);
|
||||
maze[k][l] = 'Z';
|
||||
printf("%c" , maze[k][l]);
|
||||
break;
|
||||
|
||||
case 'T':
|
||||
fscanf(mazeFile, "%d %d", &k, &l);
|
||||
maze[k][l] = 'T';
|
||||
break;
|
||||
case '\n':
|
||||
printf("\n");
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
};
|
||||
fgets(shold, 2, mazeFile);
|
||||
if (feof(mazeFile))
|
||||
{
|
||||
break;
|
||||
}
|
||||
}
|
||||
fclose(mazeFile);
|
||||
for (c1 =0; c1 < row; c1++)
|
||||
{
|
||||
printf ("\n");
|
||||
for (c2=0; c2 < col; c2++)
|
||||
{
|
||||
printf("%c", maze[c1][c2]);
|
||||
}
|
||||
}
|
||||
|
||||
//free the memory
|
||||
int i = 0;
|
||||
for(i = 0; i < row; i++)
|
||||
{
|
||||
free(maze[i]);
|
||||
}
|
||||
free(maze);
|
||||
printf("\n");
|
||||
|
||||
return 0;
|
||||
}
|
@ -0,0 +1,290 @@
|
||||
#include <ncurses.h>
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <stdint.h>
|
||||
#include <string.h>
|
||||
#include <unistd.h>
|
||||
|
||||
extern WINDOW* initscr(void);
|
||||
extern int start_color(void);
|
||||
|
||||
/* LDRA_EXCLUDE RULE VIOLATION <justification start> FALSE POSITIVE -- why <justification end> */
|
||||
|
||||
int main(int argc, char *argv[])
|
||||
{
|
||||
FILE* mazeFile; /* Maze Input File */
|
||||
unsigned int row = 0; /* Holds number of rows for input from file */
|
||||
unsigned int col = 0; /* Holds number of cols for input from file */
|
||||
unsigned int pcol = 0;
|
||||
unsigned int prow = 0;
|
||||
char shold[2000]; /* Holds lines for input from file */
|
||||
unsigned int l = 0;
|
||||
unsigned int k = 0;
|
||||
char **maze; /* array for maze */
|
||||
unsigned int c1 = 0;
|
||||
unsigned int c2 = 0;
|
||||
unsigned int i = 0;
|
||||
unsigned int j = 0;
|
||||
unsigned int x = 0;
|
||||
unsigned int y = 0;
|
||||
int deltaRow, deltaCol; /* Change in row-column position based on key input */
|
||||
unsigned int score = 0;
|
||||
|
||||
|
||||
WINDOW* W = 0; /* Curses Window handle */
|
||||
unsigned int rowLimit,colLimit; /* Num of rows and columns in terminal window */
|
||||
/* Initialize ncurses -- you will want to use the following settings */
|
||||
|
||||
|
||||
|
||||
/* check to see if argument */
|
||||
if (argc != 2)
|
||||
{
|
||||
/*printw("An error has ocurred no input file was given.\n");*/
|
||||
return 1;
|
||||
}
|
||||
mazeFile = fopen(argv[1], "r");
|
||||
if (mazeFile == NULL)
|
||||
{
|
||||
/*printw("Error in opening the file.\n");*/
|
||||
return 1;
|
||||
}
|
||||
fgets(shold, 2, mazeFile);
|
||||
while ( feof(mazeFile) == 0 )
|
||||
{
|
||||
|
||||
|
||||
switch(shold[0])
|
||||
{
|
||||
case 'M':
|
||||
/* read array values, dynamically allocate array */
|
||||
fscanf(mazeFile, "%3u %3u", &row, &col);
|
||||
maze = (char**)malloc(row * 8); /* */
|
||||
for(i = (unsigned) 0; i < row; i++)
|
||||
{
|
||||
if (maze==NULL)
|
||||
{
|
||||
break;
|
||||
}
|
||||
maze[i] = malloc(col * 8);
|
||||
}
|
||||
for (x = (unsigned) 0; x < row; x++)
|
||||
{
|
||||
for (j = (unsigned) 0; j < col; j++)
|
||||
{
|
||||
fscanf(mazeFile, "%c", &maze[x][j]);
|
||||
if (maze[x][j] == '\n')
|
||||
{
|
||||
fscanf(mazeFile, "%c", &maze[x][j]);
|
||||
}
|
||||
}
|
||||
}
|
||||
break;
|
||||
case 'P':
|
||||
fscanf(mazeFile, "%3u %3u", &k, &l);
|
||||
/* check maze bounds */
|
||||
if(k>= row || l>= col){
|
||||
break;
|
||||
}
|
||||
maze[k][l] = 'P';
|
||||
break;
|
||||
|
||||
case 'Z':
|
||||
fscanf(mazeFile, "%3u %3u", &k, &l);
|
||||
/* check maze bounds */
|
||||
if(k>= row || l>= col){
|
||||
break;
|
||||
}
|
||||
maze[k][l] = 'Z';
|
||||
break;
|
||||
|
||||
case 'T':
|
||||
fscanf(mazeFile, "%3u %3u", &k, &l);
|
||||
/* check maze bounds */
|
||||
if(k>= row || l>= col){
|
||||
break;
|
||||
}
|
||||
maze[k][l] = 'T';
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
};
|
||||
fgets(shold, 2, mazeFile);
|
||||
if (feof(mazeFile) != 0)
|
||||
{
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
rowLimit = row;
|
||||
colLimit = col;
|
||||
fclose(mazeFile);
|
||||
/* LDRA_EXCLUDE 170 S <justification start> FALSE POSITIVE -- needed for ncurses <justification end> */
|
||||
W = initscr(); /* Determine terminal type and initialize curses data structures */
|
||||
/* LDRA_EXCLUDE 170 S <justification start> FALSE POSITIVE -- needed for ncurses <justification end> */
|
||||
start_color(); /* Enable use of color with ncurses */
|
||||
/* LDRA_EXCLUDE 170 S <justification start> FALSE POSITIVE -- needed for ncurses <justification end> */
|
||||
cbreak(); /* Disable line buffering and make char inputs immediately accessible to program */
|
||||
/* LDRA_EXCLUDE 170 S <justification start> FALSE POSITIVE -- needed for ncurses <justification end> */
|
||||
noecho(); /* Disable echo printing of chars type by user */
|
||||
if (W == NULL)
|
||||
{
|
||||
return 0;
|
||||
}
|
||||
nodelay(W, true); /* Make getch( ) a non-blocking call */
|
||||
keypad(W, true); /* Enable keypad and arrow keys */
|
||||
curs_set(0); /* Set cursor to be invisible - 0 */
|
||||
getmaxyx(W, rowLimit, colLimit); /* Query terminal dimensions */
|
||||
|
||||
/* Define color pallete foreground-background color pairs */
|
||||
/* PairID#, Foreground Color, Background Color */
|
||||
/* Foreground color == Background color ==> solid color block */
|
||||
init_pair(1, COLOR_BLACK, COLOR_BLACK); /* Black text on Black background */
|
||||
init_pair(2, COLOR_GREEN, COLOR_GREEN); /* Red text on Black background */
|
||||
init_pair(3, COLOR_WHITE, COLOR_WHITE); /* White text on Blue background */
|
||||
init_pair(4, COLOR_RED, COLOR_RED); /* Yellow text on Yellow background */
|
||||
init_pair(5, COLOR_YELLOW, COLOR_YELLOW); /* Yellow text on Yellow background */
|
||||
|
||||
|
||||
for (c1 =(unsigned)0; c1 < row; c1++)
|
||||
{
|
||||
/*printw ("\n");*/
|
||||
for (c2=(unsigned)0; c2 < col; c2++)
|
||||
{
|
||||
if (maze==NULL)
|
||||
{
|
||||
break;
|
||||
}
|
||||
switch(maze[c1][c2])
|
||||
{
|
||||
case '#':
|
||||
/* read array values, dynamically allocate array*/
|
||||
attron(COLOR_PAIR(2));
|
||||
wmove(W, c1,c2);
|
||||
waddch(W,'#');
|
||||
attroff(COLOR_PAIR(2));
|
||||
break;
|
||||
case ' ':
|
||||
wmove(W, c1,c2);
|
||||
waddch(W,' ');
|
||||
break;
|
||||
case 'P':
|
||||
wmove(W, c1,c2);
|
||||
waddch(W,'P');
|
||||
prow=c1;
|
||||
pcol=c2;
|
||||
break;
|
||||
case 'Z':
|
||||
attron(COLOR_PAIR(4));
|
||||
wmove(W, c1,c2);
|
||||
waddch(W,'Z');
|
||||
attroff(COLOR_PAIR(4));
|
||||
break;
|
||||
case 'T':
|
||||
attron(COLOR_PAIR(5));
|
||||
wmove(W, c1,c2);
|
||||
waddch(W,'T');
|
||||
attroff(COLOR_PAIR(5));
|
||||
break;
|
||||
|
||||
default:
|
||||
break;
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
deltaRow =0;
|
||||
deltaCol =0;
|
||||
|
||||
|
||||
|
||||
do{
|
||||
|
||||
int kb = wgetch(W); /* Grab keypress */
|
||||
|
||||
/* Support player movement via WASD, IJKL, and arrow keys */
|
||||
if (kb == (int)'a' || kb == (int)'j' || kb == KEY_LEFT)
|
||||
{
|
||||
/* Move Left */
|
||||
deltaRow =0;
|
||||
deltaCol =-1;
|
||||
}
|
||||
else if (kb == (int)'d' || kb == (int)'l' || kb == KEY_RIGHT)
|
||||
{
|
||||
/* Move Right */
|
||||
deltaRow =0;
|
||||
deltaCol =1;
|
||||
}
|
||||
else if (kb == (int)'w' || kb == (int)'i' || kb == KEY_UP)
|
||||
{
|
||||
/* Move Up */
|
||||
deltaRow =-1;
|
||||
deltaCol =0;
|
||||
}
|
||||
else if (kb == (int)'s' || kb == (int)'k' || kb == KEY_DOWN)
|
||||
{
|
||||
/* Move Down */
|
||||
deltaRow =1;
|
||||
deltaCol =0;
|
||||
}
|
||||
else if (kb == (int)'q')
|
||||
{
|
||||
/* q to exit player movement loop */
|
||||
break;
|
||||
};
|
||||
|
||||
attron(COLOR_PAIR(1));
|
||||
wmove(W,prow,pcol);
|
||||
waddch(W, 'P');
|
||||
attroff(COLOR_PAIR(1));
|
||||
|
||||
if(((prow + (unsigned int)deltaRow) > row) || ((pcol + (unsigned int)deltaCol) > col)){
|
||||
deltaRow = 0;
|
||||
deltaCol = 0;
|
||||
}
|
||||
if(maze[((int)prow + deltaRow)][((int)pcol + deltaCol)] == '#'){
|
||||
prow = prow;
|
||||
pcol = pcol;
|
||||
}
|
||||
else{
|
||||
prow = prow + (unsigned int)deltaRow;
|
||||
pcol = pcol + (unsigned int)deltaCol;
|
||||
}
|
||||
if(maze[prow][pcol] == 'T')
|
||||
{
|
||||
maze[prow][pcol] = ' ';
|
||||
score++;
|
||||
}
|
||||
if(maze[prow][pcol] == 'Z')
|
||||
{
|
||||
break;
|
||||
}
|
||||
|
||||
attron(COLOR_PAIR(3));
|
||||
wmove(W,prow,pcol);
|
||||
waddch(W, 'P');
|
||||
attroff(COLOR_PAIR(3));
|
||||
|
||||
deltaRow =0;
|
||||
deltaCol =0;
|
||||
|
||||
wrefresh(W); /* Refresh ncurses window */
|
||||
|
||||
} while (true); /* Repeat until user selects q to quit */
|
||||
|
||||
/* free the memory */
|
||||
for(y = (unsigned)0; y < row; y++)
|
||||
{
|
||||
free(maze[y]);
|
||||
maze[y] = NULL;
|
||||
}
|
||||
if(maze != NULL){
|
||||
free(maze);
|
||||
}
|
||||
/*printw("\n");*/
|
||||
endwin();
|
||||
printf("Score = %u\n", score);
|
||||
return 0;
|
||||
}
|
||||
|
@ -0,0 +1,151 @@
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <stdint.h>
|
||||
#include <string.h>
|
||||
#include <unistd.h>
|
||||
#include <ncurses.h>
|
||||
|
||||
|
||||
|
||||
|
||||
int main(int argc, char *argv[])
|
||||
{
|
||||
FILE* mazeFile; /* Maze Input File */
|
||||
unsigned int row = 0; /* Holds number of rows for input from file */
|
||||
unsigned int col = 0; /* Holds number of cols for input from file */
|
||||
char shold[2000]; /* Holds lines for input from file */
|
||||
unsigned int l = 0;
|
||||
unsigned int k = 0;
|
||||
char **maze; /* array for maze */
|
||||
unsigned int c1 = 0;
|
||||
unsigned int c2 = 0;
|
||||
unsigned int i = 0;
|
||||
unsigned int j = 0;
|
||||
unsigned int x = 0;
|
||||
unsigned int y = 0;
|
||||
WINDOW* W = 0; /* Curses Window handle */
|
||||
|
||||
/* check to see if argument */
|
||||
if (argc != 2)
|
||||
{
|
||||
printf("An error has ocurred no input file was given.\n");
|
||||
return 1;
|
||||
}
|
||||
mazeFile = fopen(argv[1], "r");
|
||||
if (mazeFile == NULL)
|
||||
{
|
||||
printf("Error in opening the file.\n");
|
||||
return 1;
|
||||
}
|
||||
fgets(shold, 2, mazeFile);
|
||||
while ( feof(mazeFile) == 0 )
|
||||
{
|
||||
|
||||
switch(shold[0])
|
||||
{
|
||||
case 'M':
|
||||
/* read array values, dynamically allocate array */
|
||||
fscanf(mazeFile, "%3u %3u", &row, &col);
|
||||
maze = (char**)malloc(row * 8);
|
||||
for(i = (unsigned) 0; i < row; i++)
|
||||
{
|
||||
if (maze==NULL)
|
||||
{
|
||||
break;
|
||||
}
|
||||
maze[i] = malloc(col * 8);
|
||||
}
|
||||
for (x = (unsigned) 0; x < row; x++)
|
||||
{
|
||||
for (j = (unsigned) 0; j < col; j++)
|
||||
{
|
||||
fscanf(mazeFile, "%c", &maze[x][j]);
|
||||
if (maze[x][j] == '\n')
|
||||
{
|
||||
fscanf(mazeFile, "%c", &maze[x][j]);
|
||||
}
|
||||
}
|
||||
}
|
||||
break;
|
||||
case 'P':
|
||||
fscanf(mazeFile, "%3u %3u", &k, &l);
|
||||
/* check maze bounds */
|
||||
if(k>= row || l>= col){
|
||||
break;
|
||||
}
|
||||
maze[k][l] = 'P';
|
||||
break;
|
||||
|
||||
case 'Z':
|
||||
fscanf(mazeFile, "%3u %3u", &k, &l);
|
||||
/* check maze bounds */
|
||||
if(k>= row || l>= col){
|
||||
break;
|
||||
}
|
||||
maze[k][l] = 'Z';
|
||||
break;
|
||||
|
||||
case 'T':
|
||||
fscanf(mazeFile, "%3u %3u", &k, &l);
|
||||
/* check maze bounds */
|
||||
if(k>= row || l>= col){
|
||||
break;
|
||||
}
|
||||
maze[k][l] = 'T';
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
};
|
||||
fgets(shold, 2, mazeFile);
|
||||
if (feof(mazeFile) != 0)
|
||||
{
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
fclose(mazeFile);
|
||||
/* Initialize ncurses -- you will want to use the following settings */
|
||||
W = initscr(); /* Determine terminal type and initialize curses data structures */
|
||||
start_color(); /* Enable use of color with ncurses */
|
||||
cbreak(); /* Disable line buffering and make char inputs immediately accessible to program */
|
||||
noecho(); /* Disable echo printing of chars type by user */
|
||||
nodelay(W, true); /* Make getch( ) a non-blocking call */
|
||||
keypad(W, true); /* Enable keypad and arrow keys */
|
||||
curs_set(0); /* Set cursor to be invisible - 0 */
|
||||
getmaxyx(W, row, col); /* Query terminal dimensions */
|
||||
|
||||
/* Define color pallete foreground-background color pairs */
|
||||
/* PairID#, Foreground Color, Background Color */
|
||||
/* Foreground color == Background color ==> solid color block */
|
||||
init_pair(1, COLOR_BLACK, COLOR_BLACK); /* Black text on Black background */
|
||||
init_pair(2, COLOR_RED, COLOR_BLACK); /* Red text on Black background */
|
||||
init_pair(3, COLOR_WHITE, COLOR_BLUE); /* White text on Blue background */
|
||||
init_pair(4, COLOR_YELLOW, COLOR_YELLOW); /* Yellow text on Yellow background */
|
||||
|
||||
|
||||
for (c1 =(unsigned)0; c1 < row; c1++)
|
||||
{
|
||||
printf ("\n");
|
||||
for (c2=(unsigned)0; c2 < col; c2++)
|
||||
{
|
||||
if (maze==NULL)
|
||||
{
|
||||
break;
|
||||
}
|
||||
printf("%c", maze[c1][c2]);
|
||||
}
|
||||
}
|
||||
|
||||
/* free the memory */
|
||||
for(y = (unsigned)0; y < row; y++)
|
||||
{
|
||||
free(maze[y]);
|
||||
maze[y] = NULL;
|
||||
}
|
||||
if(maze != NULL){
|
||||
free(maze);
|
||||
}
|
||||
printf("\n");
|
||||
return 0;
|
||||
}
|
@ -0,0 +1,128 @@
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <stdint.h>
|
||||
#include <string.h>
|
||||
|
||||
|
||||
|
||||
|
||||
int main(int argc, char *argv[])
|
||||
{
|
||||
FILE* mazeFile; /* Maze Input File */
|
||||
char chold = NULL; /* Holds character for line input from file */
|
||||
unsigned int row = 0; /* Holds number of rows for input from file */
|
||||
unsigned int col = 0; /* Holds number of cols for input from file */
|
||||
char shold[2000]; /* Holds lines for input from file */
|
||||
unsigned int l = 0;
|
||||
unsigned int k = 0;
|
||||
char **maze; // global array for maze
|
||||
unsigned int c1 = 0;
|
||||
unsigned int c2 = 0;
|
||||
|
||||
// check to see if
|
||||
if (argc != 2)
|
||||
{
|
||||
printf("An error has ocurred no input file was given.\n");
|
||||
return 1;
|
||||
}
|
||||
mazeFile = fopen(argv[1], "r");
|
||||
if (mazeFile == NULL)
|
||||
{
|
||||
printf("Error in opening the file.\n");
|
||||
return 1;
|
||||
}
|
||||
fgets(shold, 2, mazeFile);
|
||||
while ( feof(mazeFile) == 0 )
|
||||
{
|
||||
|
||||
switch(shold[0])
|
||||
{
|
||||
case 'M':
|
||||
// read array values, dynamically allocate array
|
||||
fscanf(mazeFile, "%3d %3d", &row, &col);
|
||||
unsigned int i = 0, j = 0;
|
||||
maze = (char**)malloc(row * 8);
|
||||
for(i = (unsigned) 0; i < row; i++)
|
||||
{
|
||||
if (maze==NULL)
|
||||
{
|
||||
break;
|
||||
}
|
||||
maze[i] = malloc(col * 8);
|
||||
}
|
||||
for (i = (unsigned) 0; i < row; i++)
|
||||
{
|
||||
for (j = (unsigned) 0; j < col; j++)
|
||||
{
|
||||
fscanf(mazeFile, "%c", &maze[i][j]);
|
||||
if (maze[i][j] == '\n')
|
||||
{
|
||||
fscanf(mazeFile, "%c", &maze[i][j]);
|
||||
}
|
||||
}
|
||||
}
|
||||
break;
|
||||
case 'P':
|
||||
fscanf(mazeFile, "%3d %3d", &k, &l);
|
||||
// check maze bounds
|
||||
if(k>= row || l>= col){
|
||||
break;
|
||||
}
|
||||
maze[k][l] = 'P';
|
||||
break;
|
||||
|
||||
case 'Z':
|
||||
fscanf(mazeFile, "%3d %3d", &k, &l);
|
||||
// check maze bounds
|
||||
if(k>= row || l>= col){
|
||||
break;
|
||||
}
|
||||
maze[k][l] = 'Z';
|
||||
break;
|
||||
|
||||
case 'T':
|
||||
fscanf(mazeFile, "%3d %3d", &k, &l);
|
||||
// check maze bounds
|
||||
if(k>= row || l>= col){
|
||||
break;
|
||||
}
|
||||
maze[k][l] = 'T';
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
};
|
||||
fgets(shold, 2, mazeFile);
|
||||
if (feof(mazeFile) != 0)
|
||||
{
|
||||
break;
|
||||
}
|
||||
}
|
||||
fclose(mazeFile);
|
||||
// print out maze
|
||||
for (c1 =(unsigned)0; c1 < row; c1++)
|
||||
{
|
||||
printf ("\n");
|
||||
for (c2=(unsigned)0; c2 < col; c2++)
|
||||
{
|
||||
if (maze==NULL)
|
||||
{
|
||||
break;
|
||||
}
|
||||
printf("%c", maze[c1][c2]);
|
||||
}
|
||||
}
|
||||
|
||||
//free the memory
|
||||
unsigned int i = 0;
|
||||
for(i = (unsigned)0; i < row; i++)
|
||||
{
|
||||
free(maze[i]);
|
||||
maze[i] = NULL;
|
||||
}
|
||||
if(maze != NULL){
|
||||
free(maze);
|
||||
}
|
||||
printf("\n");
|
||||
|
||||
return 0;
|
||||
}
|
@ -0,0 +1,128 @@
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <stdint.h>
|
||||
#include <string.h>
|
||||
|
||||
|
||||
|
||||
|
||||
int main(int argc, char *argv[])
|
||||
{
|
||||
FILE* mazeFile; /* Maze Input File */
|
||||
char chold = NULL; /* Holds character for line input from file */
|
||||
unsigned int row = 0; /* Holds number of rows for input from file */
|
||||
unsigned int col = 0; /* Holds number of cols for input from file */
|
||||
char shold[2000]; /* Holds lines for input from file */
|
||||
unsigned int l = 0;
|
||||
unsigned int k = 0;
|
||||
char **maze; // global array for maze
|
||||
unsigned int c1 = 0;
|
||||
unsigned int c2 = 0;
|
||||
|
||||
// check to see if
|
||||
if (argc != 2)
|
||||
{
|
||||
printf("An error has ocurred no input file was given.\n");
|
||||
return 1;
|
||||
}
|
||||
mazeFile = fopen(argv[1], "r");
|
||||
if (mazeFile == NULL)
|
||||
{
|
||||
printf("Error in opening the file.\n");
|
||||
return 1;
|
||||
}
|
||||
fgets(shold, 2, mazeFile);
|
||||
while ( feof(mazeFile) == 0 )
|
||||
{
|
||||
|
||||
switch(shold[0])
|
||||
{
|
||||
case 'M':
|
||||
// read array values, dynamically allocate array
|
||||
fscanf(mazeFile, "%3d %3d", &row, &col);
|
||||
unsigned int i = 0, j = 0;
|
||||
maze = (char**)malloc(row * sizeof(unsigned int *));
|
||||
for(i = (unsigned) 0; i < row; i++)
|
||||
{
|
||||
if (maze==NULL)
|
||||
{
|
||||
break;
|
||||
}
|
||||
maze[i] = malloc(col * sizeof(unsigned int *));
|
||||
}
|
||||
for (i = (unsigned) 0; i < row; i++)
|
||||
{
|
||||
for (j = (unsigned) 0; j < col; j++)
|
||||
{
|
||||
fscanf(mazeFile, "%c", &maze[i][j]);
|
||||
if (maze[i][j] == '\n')
|
||||
{
|
||||
fscanf(mazeFile, "%c", &maze[i][j]);
|
||||
}
|
||||
}
|
||||
}
|
||||
break;
|
||||
case 'P':
|
||||
fscanf(mazeFile, "%3d %3d", &k, &l);
|
||||
// check maze bounds
|
||||
if(k>= row || l>= col){
|
||||
break;
|
||||
}
|
||||
maze[k][l] = 'P';
|
||||
break;
|
||||
|
||||
case 'Z':
|
||||
fscanf(mazeFile, "%3d %3d", &k, &l);
|
||||
// check maze bounds
|
||||
if(k>= row || l>= col){
|
||||
break;
|
||||
}
|
||||
maze[k][l] = 'Z';
|
||||
break;
|
||||
|
||||
case 'T':
|
||||
fscanf(mazeFile, "%3d %3d", &k, &l);
|
||||
// check maze bounds
|
||||
if(k>= row || l>= col){
|
||||
break;
|
||||
}
|
||||
maze[k][l] = 'T';
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
};
|
||||
fgets(shold, 2, mazeFile);
|
||||
if (feof(mazeFile) != 0)
|
||||
{
|
||||
break;
|
||||
}
|
||||
}
|
||||
fclose(mazeFile);
|
||||
// print out maze
|
||||
for (c1 =(unsigned)0; c1 < row; c1++)
|
||||
{
|
||||
printf ("\n");
|
||||
for (c2=(unsigned)0; c2 < col; c2++)
|
||||
{
|
||||
if (maze==NULL)
|
||||
{
|
||||
break;
|
||||
}
|
||||
printf("%c", maze[c1][c2]);
|
||||
}
|
||||
}
|
||||
|
||||
//free the memory
|
||||
unsigned int i = 0;
|
||||
for(i = (unsigned)0; i < row; i++)
|
||||
{
|
||||
free(maze[i]);
|
||||
maze[i] = NULL;
|
||||
}
|
||||
if(maze != NULL){
|
||||
free(maze);
|
||||
}
|
||||
printf("\n");
|
||||
|
||||
return 0;
|
||||
}
|
@ -0,0 +1,110 @@
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <stdint.h>
|
||||
#include <string.h>
|
||||
|
||||
|
||||
// global array for maze
|
||||
char **maze;
|
||||
|
||||
|
||||
int main(int argc, char *argv[])
|
||||
{
|
||||
FILE* mazeFile; /* Maze Input File */
|
||||
char chold; /* Holds character for line input from file */
|
||||
int row = 0; /* Holds number of rows for input from file */
|
||||
int col = 0; /* Holds number of cols for input from file */
|
||||
char shold[2000]; /* Holds lines for input from file */
|
||||
int l = 0;
|
||||
int k = 0;
|
||||
int c1 = 0;
|
||||
int c2 = 0;
|
||||
|
||||
// check to see if
|
||||
if (argc != 2)
|
||||
{
|
||||
printf("An error has ocurred no input file was given.\n");
|
||||
return 1;
|
||||
}
|
||||
mazeFile = fopen(argv[1], "r");
|
||||
if (mazeFile == NULL)
|
||||
{
|
||||
printf("Error in opening the file.\n");
|
||||
return 1;
|
||||
}
|
||||
fgets(shold, 2, mazeFile);
|
||||
while ( !feof(mazeFile) )
|
||||
{
|
||||
|
||||
switch(shold[0])
|
||||
{
|
||||
case 'M':
|
||||
fscanf(mazeFile, "%d %d", &row, &col);
|
||||
// possible dynamic allocation of 2D array
|
||||
// dynamic allocation of array for maze
|
||||
int i = 0, j = 0;
|
||||
maze = malloc(row * sizeof(char *));
|
||||
for(i = 0; i < row; i++)
|
||||
{
|
||||
maze[i] = malloc(col * sizeof(char));
|
||||
}
|
||||
for (i = 0; i < row; i++)
|
||||
{
|
||||
for (j = 0; j < col; j++)
|
||||
{
|
||||
fscanf(mazeFile, "%c", &maze[i][j]);
|
||||
if (maze[i][j] == '\n')
|
||||
{
|
||||
fscanf(mazeFile, "%c", &maze[i][j]);
|
||||
}
|
||||
}
|
||||
}
|
||||
break;
|
||||
case 'P':
|
||||
fscanf(mazeFile, "%d %d", &k, &l);
|
||||
maze[k][l] = 'P';
|
||||
break;
|
||||
|
||||
case 'Z':
|
||||
fscanf(mazeFile, "%d %d", &k, &l);
|
||||
maze[k][l] = 'Z';
|
||||
printf("%c" , maze[k][l]);
|
||||
break;
|
||||
|
||||
case 'T':
|
||||
fscanf(mazeFile, "%d %d", &k, &l);
|
||||
maze[k][l] = 'T';
|
||||
break;
|
||||
case '\n':
|
||||
printf("\n");
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
};
|
||||
fgets(shold, 2, mazeFile);
|
||||
if (feof(mazeFile))
|
||||
{
|
||||
break;
|
||||
}
|
||||
}
|
||||
fclose(mazeFile);
|
||||
for (c1 =0; c1 < row; c1++)
|
||||
{
|
||||
printf ("\n");
|
||||
for (c2=0; c2 < col; c2++)
|
||||
{
|
||||
printf("%c", maze[c1][c2]);
|
||||
}
|
||||
}
|
||||
|
||||
//free the memory
|
||||
int i = 0;
|
||||
for(i = 0; i < row; i++)
|
||||
{
|
||||
free(maze[i]);
|
||||
}
|
||||
//free(maze);
|
||||
printf("\n");
|
||||
|
||||
return 0;
|
||||
}
|
@ -0,0 +1,127 @@
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <stdint.h>
|
||||
#include <string.h>
|
||||
#include <ncurses.h>
|
||||
|
||||
|
||||
|
||||
|
||||
int main(int argc, char *argv[])
|
||||
{
|
||||
FILE* mazeFile; /* Maze Input File */
|
||||
unsigned int row = 0; /* Holds number of rows for input from file */
|
||||
unsigned int col = 0; /* Holds number of cols for input from file */
|
||||
char shold[2000]; /* Holds lines for input from file */
|
||||
unsigned int l = 0;
|
||||
unsigned int k = 0;
|
||||
char **maze; /* array for maze */
|
||||
unsigned int c1 = 0;
|
||||
unsigned int c2 = 0;
|
||||
unsigned int i = 0;
|
||||
unsigned int j = 0;
|
||||
|
||||
/* check to see if argument */
|
||||
if (argc != 2)
|
||||
{
|
||||
printf("An error has ocurred no input file was given.\n");
|
||||
return 1;
|
||||
}
|
||||
mazeFile = fopen(argv[1], "r");
|
||||
if (mazeFile == NULL)
|
||||
{
|
||||
printf("Error in opening the file.\n");
|
||||
return 1;
|
||||
}
|
||||
fgets(shold, 2, mazeFile);
|
||||
while ( feof(mazeFile) == 0 )
|
||||
{
|
||||
|
||||
switch(shold[0])
|
||||
{
|
||||
case 'M':
|
||||
/* read array values, dynamically allocate array */
|
||||
fscanf(mazeFile, "%3u %3u", &row, &col);
|
||||
maze = (char**)malloc(row * 8);
|
||||
for(i = (unsigned) 0; i < row; i++)
|
||||
{
|
||||
if (maze==NULL)
|
||||
{
|
||||
break;
|
||||
}
|
||||
maze[i] = malloc(col * 8);
|
||||
}
|
||||
for (i = (unsigned) 0; i < row; i++)
|
||||
{
|
||||
for (j = (unsigned) 0; j < col; j++)
|
||||
{
|
||||
fscanf(mazeFile, "%c", &maze[i][j]);
|
||||
if (maze[i][j] == '\n')
|
||||
{
|
||||
fscanf(mazeFile, "%c", &maze[i][j]);
|
||||
}
|
||||
}
|
||||
}
|
||||
break;
|
||||
case 'P':
|
||||
fscanf(mazeFile, "%3u %3u", &k, &l);
|
||||
/* check maze bounds */
|
||||
if(k>= row || l>= col){
|
||||
break;
|
||||
}
|
||||
maze[k][l] = 'P';
|
||||
break;
|
||||
|
||||
case 'Z':
|
||||
fscanf(mazeFile, "%3u %3u", &k, &l);
|
||||
/* check maze bounds */
|
||||
if(k>= row || l>= col){
|
||||
break;
|
||||
}
|
||||
maze[k][l] = 'Z';
|
||||
break;
|
||||
|
||||
case 'T':
|
||||
fscanf(mazeFile, "%3u %3u", &k, &l);
|
||||
/* check maze bounds */
|
||||
if(k>= row || l>= col){
|
||||
break;
|
||||
}
|
||||
maze[k][l] = 'T';
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
};
|
||||
fgets(shold, 2, mazeFile);
|
||||
if (feof(mazeFile) != 0)
|
||||
{
|
||||
break;
|
||||
}
|
||||
}
|
||||
fclose(mazeFile);
|
||||
/* print out maze */
|
||||
for (c1 =(unsigned)0; c1 < row; c1++)
|
||||
{
|
||||
printf ("\n");
|
||||
for (c2=(unsigned)0; c2 < col; c2++)
|
||||
{
|
||||
if (maze==NULL)
|
||||
{
|
||||
break;
|
||||
}
|
||||
printf("%c", maze[c1][c2]);
|
||||
}
|
||||
}
|
||||
|
||||
/* free the memory */
|
||||
for(i = (unsigned)0; i < row; i++)
|
||||
{
|
||||
free(maze[i]);
|
||||
maze[i] = NULL;
|
||||
}
|
||||
if(maze != NULL){
|
||||
free(maze);
|
||||
}
|
||||
printf("\n");
|
||||
return 0;
|
||||
}
|
@ -0,0 +1,427 @@
|
||||
#include <ncurses.h>
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <stdint.h>
|
||||
#include <string.h>
|
||||
#include <unistd.h>
|
||||
|
||||
extern WINDOW* initscr(void);
|
||||
extern int start_color(void);
|
||||
extern int noecho(void);
|
||||
|
||||
|
||||
/* LDRA_EXCLUDE RULE VIOLATION <justification start> FALSE POSITIVE -- why <justification end> */
|
||||
|
||||
int main(int argc, char *argv[])
|
||||
{
|
||||
FILE* mazeFile; /* Maze Input File */
|
||||
unsigned int row = 0; /* Holds number of rows for input from file */
|
||||
unsigned int col = 0; /* Holds number of cols for input from file */
|
||||
unsigned int pcol = 0;
|
||||
unsigned int prow = 0;
|
||||
char shold[2000]; /* Holds lines for input from file */
|
||||
unsigned int l = 0;
|
||||
unsigned int k = 0;
|
||||
char **maze; /* array for maze */
|
||||
unsigned int c1 = 0;
|
||||
unsigned int c2 = 0;
|
||||
unsigned int i = 0;
|
||||
unsigned int j = 0;
|
||||
unsigned int x = 0;
|
||||
unsigned int y = 0;
|
||||
int deltaRow, deltaCol; /* Change in row-column position based on key input */
|
||||
unsigned int score = 0;
|
||||
|
||||
|
||||
WINDOW* W = 0; /* Curses Window handle */
|
||||
unsigned int rowLimit,colLimit; /* Num of rows and columns in terminal window */
|
||||
/* Initialize ncurses -- you will want to use the following settings */
|
||||
|
||||
|
||||
|
||||
/* check to see if argument */
|
||||
if (argc != 2)
|
||||
{
|
||||
/*printw("An error has ocurred no input file was given.\n");*/
|
||||
return 1;
|
||||
}
|
||||
/* LDRA_EXCLUDE 44 S <justification start> FALSE POSITIVE -- Need function to open the file so must use bad one bc we cannot create one ourselves as we are not wizards <justification end> */
|
||||
mazeFile = fopen(argv[1], "r");
|
||||
if (mazeFile == NULL)
|
||||
{
|
||||
/*printw("Error in opening the file.\n");*/
|
||||
return 1;
|
||||
}
|
||||
/* LDRA_EXCLUDE 44 S <justification start> FALSE POSITIVE -- Need function to get first char from file and we must use bad one as we cannot make one ourselves <justification end> */
|
||||
fgets(shold, 2, mazeFile);
|
||||
while ( feof(mazeFile) == 0 )
|
||||
{
|
||||
|
||||
/* parse maze file,
|
||||
adding chars to array */
|
||||
switch(shold[0])
|
||||
{
|
||||
case 'M':
|
||||
/* read array values, dynamically allocate array */
|
||||
/* LDRA_EXCLUDE 44 S <justification start> FALSE POSITIVE -- Need function to scan content from file, have ensured we will not scan extra <justification end> */
|
||||
fscanf(mazeFile, "%3u %3u", &row, &col);
|
||||
maze = (char**)malloc(row * 8); /* allocate memory for rows of array */
|
||||
for(i = (unsigned) 0; i < row; i++)
|
||||
{
|
||||
if (maze==NULL)
|
||||
{
|
||||
break;
|
||||
}
|
||||
maze[i] = malloc(col * 8); /* allocate memory for collumns of array */
|
||||
}
|
||||
for (x = (unsigned) 0; x < row; x++)
|
||||
{
|
||||
for (j = (unsigned) 0; j < col; j++)
|
||||
{
|
||||
/* LDRA_EXCLUDE 44 S <justification start> FALSE POSITIVE -- Need function to scan content from file, have ensured we will not scan extra <justification end> */
|
||||
fscanf(mazeFile, "%c", &maze[x][j]);
|
||||
if (maze[x][j] == '\n')
|
||||
{
|
||||
/* LDRA_EXCLUDE 44 S <justification start> FALSE POSITIVE -- Need function to scan content from file, have ensured we will not scan extra <justification end> */
|
||||
fscanf(mazeFile, "%c", &maze[x][j]);
|
||||
}
|
||||
}
|
||||
}
|
||||
break;
|
||||
case 'P':
|
||||
/* LDRA_EXCLUDE 44 S <justification start> FALSE POSITIVE -- Need function to scan content from file, have ensured we will not scan extra <justification end> */
|
||||
fscanf(mazeFile, "%3u %3u", &k, &l);
|
||||
/* check maze bounds */
|
||||
if(k>= row || l>= col){
|
||||
break;
|
||||
}
|
||||
maze[k][l] = 'P';
|
||||
break;
|
||||
|
||||
case 'Z':
|
||||
/* LDRA_EXCLUDE 44 S <justification start> FALSE POSITIVE -- Need function to scan content from file, have ensured we will not scan extra <justification end> */
|
||||
fscanf(mazeFile, "%3u %3u", &k, &l);
|
||||
/* check maze bounds */
|
||||
if(k>= row || l>= col){
|
||||
break;
|
||||
}
|
||||
maze[k][l] = 'Z';
|
||||
break;
|
||||
|
||||
case 'T':
|
||||
/* LDRA_EXCLUDE 44 S <justification start> FALSE POSITIVE -- Need function to scan content from file, have ensured we will not scan extra <justification end> */
|
||||
fscanf(mazeFile, "%3u %3u", &k, &l);
|
||||
/* check maze bounds */
|
||||
if(k>= row || l>= col){
|
||||
break;
|
||||
}
|
||||
maze[k][l] = 'T';
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
};
|
||||
/* LDRA_EXCLUDE 44 S <justification start> FALSE POSITIVE -- Need function to scan content from file, have ensured we will not scan extra <justification end> */
|
||||
fgets(shold, 2, mazeFile);
|
||||
if (feof(mazeFile) != 0)
|
||||
{
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
rowLimit = row;
|
||||
colLimit = col;
|
||||
fclose(mazeFile);
|
||||
/* LDRA_EXCLUDE 170 S <justification start> FALSE POSITIVE -- needed for ncurses <justification end> */
|
||||
/* LDRA_EXCLUDE 496 S <justification start> FALSE POSITIVE -- needed for ncurses <justification end> */
|
||||
/* LDRA_EXCLUDE 41 D <justification start> FALSE POSITIVE -- needed for ncurses <justification end> */
|
||||
W = initscr(); /* Determine terminal type and initialize curses data structures */
|
||||
/* LDRA_EXCLUDE 170 S <justification start> FALSE POSITIVE -- needed for ncurses <justification end> */
|
||||
/* LDRA_EXCLUDE 496 S <justification start> FALSE POSITIVE -- needed for ncurses <justification end> */
|
||||
/* LDRA_EXCLUDE 41 D <justification start> FALSE POSITIVE -- needed for ncurses <justification end> */
|
||||
start_color(); /* Enable use of color with ncurses */
|
||||
/* LDRA_EXCLUDE 170 S <justification start> FALSE POSITIVE -- needed for ncurses <justification end> */
|
||||
/* LDRA_EXCLUDE 496 S <justification start> FALSE POSITIVE -- needed for ncurses <justification end> */
|
||||
/* LDRA_EXCLUDE 41 D <justification start> FALSE POSITIVE -- needed for ncurses <justification end> */
|
||||
cbreak(); /* Disable line buffering and make char inputs immediately accessible to program */
|
||||
/* LDRA_EXCLUDE 170 S <justification start> FALSE POSITIVE -- needed for ncurses <justification end> */
|
||||
/* LDRA_EXCLUDE 496 S <justification start> FALSE POSITIVE -- needed for ncurses <justification end> */
|
||||
/* LDRA_EXCLUDE 41 D <justification start> FALSE POSITIVE -- needed for ncurses <justification end> */
|
||||
noecho(); /* Disable echo printing of chars type by user */
|
||||
if (W == NULL)
|
||||
{
|
||||
return 0;
|
||||
}
|
||||
/* LDRA_EXCLUDE 170 S <justification start> FALSE POSITIVE -- needed for ncurses <justification end> */
|
||||
/* LDRA_EXCLUDE 496 S <justification start> FALSE POSITIVE -- needed for ncurses <justification end> */
|
||||
/* LDRA_EXCLUDE 41 D <justification start> FALSE POSITIVE -- needed for ncurses <justification end> */
|
||||
nodelay(W, true); /* Make getch( ) a non-blocking call */
|
||||
/* LDRA_EXCLUDE 170 S <justification start> FALSE POSITIVE -- needed for ncurses <justification end> */
|
||||
/* LDRA_EXCLUDE 496 S <justification start> FALSE POSITIVE -- needed for ncurses <justification end> */
|
||||
/* LDRA_EXCLUDE 41 D <justification start> FALSE POSITIVE -- needed for ncurses <justification end> */
|
||||
keypad(W, true); /* Enable keypad and arrow keys */
|
||||
/* LDRA_EXCLUDE 170 S <justification start> FALSE POSITIVE -- needed for ncurses <justification end> */
|
||||
/* LDRA_EXCLUDE 496 S <justification start> FALSE POSITIVE -- needed for ncurses <justification end> */
|
||||
/* LDRA_EXCLUDE 41 D <justification start> FALSE POSITIVE -- needed for ncurses <justification end> */
|
||||
curs_set(0); /* Set cursor to be invisible - 0 */
|
||||
/* LDRA_EXCLUDE 170 S <justification start> FALSE POSITIVE -- needed for ncurses <justification end> */
|
||||
/* LDRA_EXCLUDE 496 S <justification start> FALSE POSITIVE -- needed for ncurses <justification end> */
|
||||
/* LDRA_EXCLUDE 41 D <justification start> FALSE POSITIVE -- needed for ncurses <justification end> */
|
||||
getmaxyx(W, rowLimit, colLimit); /* Query terminal dimensions */
|
||||
|
||||
/* Define color pallete foreground-background color pairs */
|
||||
/* PairID#, Foreground Color, Background Color */
|
||||
/* Foreground color == Background color ==> solid color block */
|
||||
/* LDRA_EXCLUDE 170 S <justification start> FALSE POSITIVE -- needed for ncurses <justification end> */
|
||||
/* LDRA_EXCLUDE 496 S <justification start> FALSE POSITIVE -- needed for ncurses <justification end> */
|
||||
/* LDRA_EXCLUDE 41 D <justification start> FALSE POSITIVE -- needed for ncurses <justification end> */
|
||||
init_pair(1, COLOR_BLACK, COLOR_BLACK); /* Black text on Black background */
|
||||
/* LDRA_EXCLUDE 170 S <justification start> FALSE POSITIVE -- needed for ncurses <justification end> */
|
||||
/* LDRA_EXCLUDE 496 S <justification start> FALSE POSITIVE -- needed for ncurses <justification end> */
|
||||
init_pair(2, COLOR_GREEN, COLOR_GREEN); /* Red text on Black background */
|
||||
/* LDRA_EXCLUDE 170 S <justification start> FALSE POSITIVE -- needed for ncurses <justification end> */
|
||||
/* LDRA_EXCLUDE 496 S <justification start> FALSE POSITIVE -- needed for ncurses <justification end> */
|
||||
init_pair(3, COLOR_WHITE, COLOR_WHITE); /* White text on Blue background */
|
||||
/* LDRA_EXCLUDE 170 S <justification start> FALSE POSITIVE -- needed for ncurses <justification end> */
|
||||
/* LDRA_EXCLUDE 496 S <justification start> FALSE POSITIVE -- needed for ncurses <justification end> */
|
||||
init_pair(4, COLOR_RED, COLOR_RED); /* Yellow text on Yellow background */
|
||||
/* LDRA_EXCLUDE 170 S <justification start> FALSE POSITIVE -- needed for ncurses <justification end> */
|
||||
/* LDRA_EXCLUDE 496 S <justification start> FALSE POSITIVE -- needed for ncurses <justification end> */
|
||||
init_pair(5, COLOR_YELLOW, COLOR_YELLOW); /* Yellow text on Yellow background */
|
||||
|
||||
|
||||
for (c1 =(unsigned)0; c1 < row; c1++)
|
||||
{
|
||||
/*printw ("\n");*/
|
||||
for (c2=(unsigned)0; c2 < col; c2++)
|
||||
{
|
||||
if (maze==NULL)
|
||||
{
|
||||
break;
|
||||
}
|
||||
/* parse maze */
|
||||
switch(maze[c1][c2])
|
||||
{
|
||||
case '#':
|
||||
/* read array values, dynamically allocate array*/
|
||||
/* LDRA_EXCLUDE 170 S <justification start> FALSE POSITIVE -- needed for ncurses <justification end> */
|
||||
/* LDRA_EXCLUDE 170 S <justification start> FALSE POSITIVE -- needed for ncurses <justification end> */
|
||||
/* LDRA_EXCLUDE 496 S <justification start> FALSE POSITIVE -- needed for ncurses <justification end> */
|
||||
/* LDRA_EXCLUDE 496 S <justification start> FALSE POSITIVE -- needed for ncurses <justification end> */
|
||||
/* LDRA_EXCLUDE 41 D <justification start> FALSE POSITIVE -- needed for ncurses <justification end> */
|
||||
/* LDRA_EXCLUDE 41 D <justification start> FALSE POSITIVE -- needed for ncurses <justification end> */
|
||||
attron(COLOR_PAIR(2));
|
||||
/* LDRA_EXCLUDE 170 S <justification start> FALSE POSITIVE -- needed for ncurses <justification end> */
|
||||
/* LDRA_EXCLUDE 496 S <justification start> FALSE POSITIVE -- needed for ncurses <justification end> */
|
||||
wmove(W, c1,c2);
|
||||
/* LDRA_EXCLUDE 170 S <justification start> FALSE POSITIVE -- needed for ncurses <justification end> */
|
||||
/* LDRA_EXCLUDE 496 S <justification start> FALSE POSITIVE -- needed for ncurses <justification end> */
|
||||
waddch(W,'#');
|
||||
/* LDRA_EXCLUDE 170 S <justification start> FALSE POSITIVE -- needed for ncurses <justification end> */
|
||||
/* LDRA_EXCLUDE 170 S <justification start> FALSE POSITIVE -- needed for ncurses <justification end> */
|
||||
/* LDRA_EXCLUDE 496 S <justification start> FALSE POSITIVE -- needed for ncurses <justification end> */
|
||||
/* LDRA_EXCLUDE 496 S <justification start> FALSE POSITIVE -- needed for ncurses <justification end> */
|
||||
/* LDRA_EXCLUDE 41 D <justification start> FALSE POSITIVE -- needed for ncurses <justification end> */
|
||||
attroff(COLOR_PAIR(2));
|
||||
break;
|
||||
case ' ':
|
||||
/* LDRA_EXCLUDE 170 S <justification start> FALSE POSITIVE -- needed for ncurses <justification end> */
|
||||
/* LDRA_EXCLUDE 496 S <justification start> FALSE POSITIVE -- needed for ncurses <justification end> */
|
||||
wmove(W, c1,c2);
|
||||
/* LDRA_EXCLUDE 170 S <justification start> FALSE POSITIVE -- needed for ncurses <justification end> */
|
||||
/* LDRA_EXCLUDE 496 S <justification start> FALSE POSITIVE -- needed for ncurses <justification end> */
|
||||
waddch(W,' ');
|
||||
break;
|
||||
case 'P':
|
||||
/* LDRA_EXCLUDE 170 S <justification start> FALSE POSITIVE -- needed for ncurses <justification end> */
|
||||
/* LDRA_EXCLUDE 496 S <justification start> FALSE POSITIVE -- needed for ncurses <justification end> */
|
||||
wmove(W, c1,c2);
|
||||
/* LDRA_EXCLUDE 170 S <justification start> FALSE POSITIVE -- needed for ncurses <justification end> */
|
||||
/* LDRA_EXCLUDE 496 S <justification start> FALSE POSITIVE -- needed for ncurses <justification end> */
|
||||
waddch(W,'P');
|
||||
prow=c1;
|
||||
pcol=c2;
|
||||
break;
|
||||
case 'Z':
|
||||
/* LDRA_EXCLUDE 170 S <justification start> FALSE POSITIVE -- needed for ncurses <justification end> */
|
||||
/* LDRA_EXCLUDE 170 S <justification start> FALSE POSITIVE -- needed for ncurses <justification end> */
|
||||
/* LDRA_EXCLUDE 496 S <justification start> FALSE POSITIVE -- needed for ncurses <justification end> */
|
||||
/* LDRA_EXCLUDE 496 S <justification start> FALSE POSITIVE -- needed for ncurses <justification end> */
|
||||
attron(COLOR_PAIR(4));
|
||||
/* LDRA_EXCLUDE 170 S <justification start> FALSE POSITIVE -- needed for ncurses <justification end> */
|
||||
/* LDRA_EXCLUDE 496 S <justification start> FALSE POSITIVE -- needed for ncurses <justification end> */
|
||||
wmove(W, c1,c2);
|
||||
/* LDRA_EXCLUDE 170 S <justification start> FALSE POSITIVE -- needed for ncurses <justification end> */
|
||||
/* LDRA_EXCLUDE 496 S <justification start> FALSE POSITIVE -- needed for ncurses <justification end> */
|
||||
waddch(W,'Z');
|
||||
/* LDRA_EXCLUDE 170 S <justification start> FALSE POSITIVE -- needed for ncurses <justification end> */
|
||||
/* LDRA_EXCLUDE 170 S <justification start> FALSE POSITIVE -- needed for ncurses <justification end> */
|
||||
/* LDRA_EXCLUDE 496 S <justification start> FALSE POSITIVE -- needed for ncurses <justification end> */
|
||||
/* LDRA_EXCLUDE 496 S <justification start> FALSE POSITIVE -- needed for ncurses <justification end> */
|
||||
attroff(COLOR_PAIR(4));
|
||||
break;
|
||||
case 'T':
|
||||
/* LDRA_EXCLUDE 170 S <justification start> FALSE POSITIVE -- needed for ncurses <justification end> */
|
||||
/* LDRA_EXCLUDE 170 S <justification start> FALSE POSITIVE -- needed for ncurses <justification end> */
|
||||
/* LDRA_EXCLUDE 496 S <justification start> FALSE POSITIVE -- needed for ncurses <justification end> */
|
||||
/* LDRA_EXCLUDE 496 S <justification start> FALSE POSITIVE -- needed for ncurses <justification end> */
|
||||
attron(COLOR_PAIR(5));
|
||||
/* LDRA_EXCLUDE 170 S <justification start> FALSE POSITIVE -- needed for ncurses <justification end> */
|
||||
/* LDRA_EXCLUDE 496 S <justification start> FALSE POSITIVE -- needed for ncurses <justification end> */
|
||||
wmove(W, c1,c2);
|
||||
/* LDRA_EXCLUDE 170 S <justification start> FALSE POSITIVE -- needed for ncurses <justification end> */
|
||||
/* LDRA_EXCLUDE 496 S <justification start> FALSE POSITIVE -- needed for ncurses <justification end> */
|
||||
waddch(W,'T');
|
||||
/* LDRA_EXCLUDE 170 S <justification start> FALSE POSITIVE -- needed for ncurses <justification end> */
|
||||
/* LDRA_EXCLUDE 170 S <justification start> FALSE POSITIVE -- needed for ncurses <justification end> */
|
||||
/* LDRA_EXCLUDE 496 S <justification start> FALSE POSITIVE -- needed for ncurses <justification end> */
|
||||
/* LDRA_EXCLUDE 496 S <justification start> FALSE POSITIVE -- needed for ncurses <justification end> */
|
||||
attroff(COLOR_PAIR(5));
|
||||
break;
|
||||
|
||||
default:
|
||||
break;
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
deltaRow = 0;
|
||||
deltaCol = 0;
|
||||
|
||||
|
||||
|
||||
do{
|
||||
|
||||
/* LDRA_EXCLUDE 170 S <justification start> FALSE POSITIVE -- needed for ncurses <justification end> */
|
||||
/* LDRA_EXCLUDE 496 S <justification start> FALSE POSITIVE -- needed for ncurses <justification end> */
|
||||
int kb = wgetch(W); /* Grab keypress */
|
||||
|
||||
/* Support player movement via WASD, IJKL, and arrow keys */
|
||||
if (kb == (int)'a' || kb == (int)'j' || kb == KEY_LEFT)
|
||||
{
|
||||
/* Move Left */
|
||||
deltaRow =0;
|
||||
deltaCol =-1;
|
||||
}
|
||||
else if (kb == (int)'d' || kb == (int)'l' || kb == KEY_RIGHT)
|
||||
{
|
||||
/* Move Right */
|
||||
deltaRow =0;
|
||||
deltaCol =1;
|
||||
}
|
||||
else if (kb == (int)'w' || kb == (int)'i' || kb == KEY_UP)
|
||||
{
|
||||
/* Move Up */
|
||||
deltaRow =-1;
|
||||
deltaCol =0;
|
||||
}
|
||||
else if (kb == (int)'s' || kb == (int)'k' || kb == KEY_DOWN)
|
||||
{
|
||||
/* Move Down */
|
||||
deltaRow =1;
|
||||
deltaCol =0;
|
||||
}
|
||||
else if (kb == (int)'q')
|
||||
{
|
||||
/* q to exit player movement loop */
|
||||
break;
|
||||
};
|
||||
|
||||
/* LDRA_EXCLUDE 170 S <justification start> FALSE POSITIVE -- needed for ncurses <justification end> */
|
||||
/* LDRA_EXCLUDE 170 S <justification start> FALSE POSITIVE -- needed for ncurses <justification end> */
|
||||
/* LDRA_EXCLUDE 496 S <justification start> FALSE POSITIVE -- needed for ncurses <justification end> */
|
||||
/* LDRA_EXCLUDE 496 S <justification start> FALSE POSITIVE -- needed for ncurses <justification end> */
|
||||
attron(COLOR_PAIR(1));
|
||||
/* LDRA_EXCLUDE 170 S <justification start> FALSE POSITIVE -- needed for ncurses <justification end> */
|
||||
/* LDRA_EXCLUDE 496 S <justification start> FALSE POSITIVE -- needed for ncurses <justification end> */
|
||||
wmove(W,prow,pcol);
|
||||
/* LDRA_EXCLUDE 170 S <justification start> FALSE POSITIVE -- needed for ncurses <justification end> */
|
||||
/* LDRA_EXCLUDE 496 S <justification start> FALSE POSITIVE -- needed for ncurses <justification end> */
|
||||
waddch(W, 'P');
|
||||
/* LDRA_EXCLUDE 170 S <justification start> FALSE POSITIVE -- needed for ncurses <justification end> */
|
||||
/* LDRA_EXCLUDE 170 S <justification start> FALSE POSITIVE -- needed for ncurses <justification end> */
|
||||
/* LDRA_EXCLUDE 496 S <justification start> FALSE POSITIVE -- needed for ncurses <justification end> */
|
||||
/* LDRA_EXCLUDE 496 S <justification start> FALSE POSITIVE -- needed for ncurses <justification end> */
|
||||
attroff(COLOR_PAIR(1));
|
||||
/* check current player position against maze bounds,
|
||||
if over the bounds, don't move */
|
||||
if(((prow + (unsigned int)deltaRow) > row) || ((pcol + (unsigned int)deltaCol) > col)){
|
||||
deltaRow = 0;
|
||||
deltaCol = 0;
|
||||
}
|
||||
/* check future player position to see if wall;
|
||||
if it is, stay in the same spot
|
||||
else move to new postion */
|
||||
if(maze[((int)prow + deltaRow)][((int)pcol + deltaCol)] == '#'){
|
||||
prow = prow;
|
||||
pcol = pcol;
|
||||
}
|
||||
else{
|
||||
prow = prow + (unsigned int)deltaRow;
|
||||
pcol = pcol + (unsigned int)deltaCol;
|
||||
}
|
||||
/* check curremt position for treasure,
|
||||
make treasure position a space,
|
||||
and increment score */
|
||||
if(maze[prow][pcol] == 'T')
|
||||
{
|
||||
maze[prow][pcol] = ' ';
|
||||
score++;
|
||||
}
|
||||
/* check curremt position for zombie,
|
||||
end game */
|
||||
if(maze[prow][pcol] == 'Z')
|
||||
{
|
||||
break;
|
||||
}
|
||||
|
||||
/* LDRA_EXCLUDE 170 S <justification start> FALSE POSITIVE -- needed for ncurses <justification end> */
|
||||
/* LDRA_EXCLUDE 170 S <justification start> FALSE POSITIVE -- needed for ncurses <justification end> */
|
||||
/* LDRA_EXCLUDE 496 S <justification start> FALSE POSITIVE -- needed for ncurses <justification end> */
|
||||
/* LDRA_EXCLUDE 496 S <justification start> FALSE POSITIVE -- needed for ncurses <justification end> */
|
||||
attron(COLOR_PAIR(3));
|
||||
/* LDRA_EXCLUDE 170 S <justification start> FALSE POSITIVE -- needed for ncurses <justification end> */
|
||||
/* LDRA_EXCLUDE 496 S <justification start> FALSE POSITIVE -- needed for ncurses <justification end> */
|
||||
wmove(W,prow,pcol);
|
||||
/* LDRA_EXCLUDE 170 S <justification start> FALSE POSITIVE -- needed for ncurses <justification end> */
|
||||
/* LDRA_EXCLUDE 496 S <justification start> FALSE POSITIVE -- needed for ncurses <justification end> */
|
||||
waddch(W, 'P');
|
||||
/* LDRA_EXCLUDE 170 S <justification start> FALSE POSITIVE -- needed for ncurses <justification end> */
|
||||
/* LDRA_EXCLUDE 170 S <justification start> FALSE POSITIVE -- needed for ncurses <justification end> */
|
||||
/* LDRA_EXCLUDE 496 S <justification start> FALSE POSITIVE -- needed for ncurses <justification end> */
|
||||
/* LDRA_EXCLUDE 496 S <justification start> FALSE POSITIVE -- needed for ncurses <justification end> */
|
||||
attroff(COLOR_PAIR(3));
|
||||
|
||||
deltaRow =0;
|
||||
deltaCol =0;
|
||||
|
||||
/* LDRA_EXCLUDE 170 S <justification start> FALSE POSITIVE -- needed for ncurses <justification end> */
|
||||
/* LDRA_EXCLUDE 496 S <justification start> FALSE POSITIVE -- needed for ncurses <justification end> */
|
||||
wrefresh(W); /* Refresh ncurses window */
|
||||
|
||||
} while (true); /* Repeat until user selects q to quit */
|
||||
|
||||
/* free the memory */
|
||||
for(y = (unsigned)0; y < row; y++)
|
||||
{
|
||||
/* LDRA_EXCLUDE 44 S <justification start> FALSE POSITIVE -- Need function to free allocated memory, cannot create one ourselves bc we are not wizards <justification end> */
|
||||
free(maze[y]);
|
||||
maze[y] = NULL;
|
||||
}
|
||||
if(maze != NULL){
|
||||
/* LDRA_EXCLUDE 44 S <justification start> FALSE POSITIVE -- Need function to free allocated memory, cannot create one ourselves bc we are not wizards <justification end> */
|
||||
free(maze);
|
||||
}
|
||||
/*printw("\n");*/
|
||||
/* LDRA_EXCLUDE 170 S <justification start> FALSE POSITIVE -- needed for ncurses <justification end> */
|
||||
/* LDRA_EXCLUDE 496 S <justification start> FALSE POSITIVE -- needed for ncurses <justification end> */
|
||||
/* LDRA_EXCLUDE 41 D <justification start> FALSE POSITIVE -- needed for ncurses <justification end> */
|
||||
endwin();
|
||||
/* LDRA_EXCLUDE 44 S <justification start> FALSE POSITIVE -- Need function to print score for project, so even though not great have to use bad one <justification end> */
|
||||
printf("Score = %u\n", score);
|
||||
return 0;
|
||||
/* LDRA_EXCLUDE 50 D <justification start> FALSE POSITIVE -- Y was statically allocated no need to free it <justification end> */
|
||||
/* LDRA_EXCLUDE 50 D <justification start> FALSE POSITIVE -- Maze has been freed already valgrind shows no leaks error must have ocurred in analysis <justification end> */
|
||||
}
|
||||
|
@ -0,0 +1,151 @@
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <stdint.h>
|
||||
#include <string.h>
|
||||
#include <unistd.h>
|
||||
#include <ncurses.h>
|
||||
|
||||
|
||||
|
||||
|
||||
int main(int argc, char *argv[])
|
||||
{
|
||||
/*FILE* mazeFile; Maze Input File */
|
||||
unsigned int row = 0; /* Holds number of rows for input from file */
|
||||
unsigned int col = 0; /* Holds number of cols for input from file */
|
||||
char shold[2000]; /* Holds lines for input from file */
|
||||
unsigned int l = 0;
|
||||
unsigned int k = 0;
|
||||
char **maze; /* array for maze */
|
||||
unsigned int c1 = 0;
|
||||
unsigned int c2 = 0;
|
||||
unsigned int i = 0;
|
||||
unsigned int j = 0;
|
||||
unsigned int x = 0;
|
||||
unsigned int y = 0;
|
||||
WINDOW* W = 0; /* Curses Window handle */
|
||||
|
||||
/* check to see if argument */
|
||||
if (argc != 2)
|
||||
{
|
||||
printw("An error has ocurred no input file was given.\n");
|
||||
return 1;
|
||||
}
|
||||
W = fopen(argv[1], "r");
|
||||
if (W == NULL)
|
||||
{
|
||||
printw("Error in opening the file.\n");
|
||||
return 1;
|
||||
}
|
||||
fgets(shold, 2, W);
|
||||
while ( feof(W) == 0 )
|
||||
{
|
||||
|
||||
switch(shold[0])
|
||||
{
|
||||
case 'M':
|
||||
/* read array values, dynamically allocate array */
|
||||
wscanw(W, "%3u %3u", &row, &col);
|
||||
maze = (char**)malloc(row * 8);
|
||||
for(i = (unsigned) 0; i < row; i++)
|
||||
{
|
||||
if (maze==NULL)
|
||||
{
|
||||
break;
|
||||
}
|
||||
maze[i] = malloc(col * 8);
|
||||
}
|
||||
for (x = (unsigned) 0; x < row; x++)
|
||||
{
|
||||
for (j = (unsigned) 0; j < col; j++)
|
||||
{
|
||||
wscanw(W, "%c", &maze[x][j]);
|
||||
if (maze[x][j] == '\n')
|
||||
{
|
||||
wscanw(W, "%c", &maze[x][j]);
|
||||
}
|
||||
}
|
||||
}
|
||||
break;
|
||||
case 'P':
|
||||
wscanw(W, "%3u %3u", &k, &l);
|
||||
/* check maze bounds */
|
||||
if(k>= row || l>= col){
|
||||
break;
|
||||
}
|
||||
maze[k][l] = 'P';
|
||||
break;
|
||||
|
||||
case 'Z':
|
||||
wscanw(W, "%3u %3u", &k, &l);
|
||||
/* check maze bounds */
|
||||
if(k>= row || l>= col){
|
||||
break;
|
||||
}
|
||||
maze[k][l] = 'Z';
|
||||
break;
|
||||
|
||||
case 'T':
|
||||
wscanw(W, "%3u %3u", &k, &l);
|
||||
/* check maze bounds */
|
||||
if(k>= row || l>= col){
|
||||
break;
|
||||
}
|
||||
maze[k][l] = 'T';
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
};
|
||||
fgets(shold, 2, W);
|
||||
if (feof(W) != 0)
|
||||
{
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
fclose(W);
|
||||
/* Initialize ncurses -- you will want to use the following settings */
|
||||
W = initscr(); /* Determine terminal type and initialize curses data structures */
|
||||
start_color(); /* Enable use of color with ncurses */
|
||||
cbreak(); /* Disable line buffering and make char inputs immediately accessible to program */
|
||||
noecho(); /* Disable echo printing of chars type by user */
|
||||
nodelay(W, true); /* Make getch( ) a non-blocking call */
|
||||
keypad(W, true); /* Enable keypad and arrow keys */
|
||||
curs_set(0); /* Set cursor to be invisible - 0 */
|
||||
getmaxyx(W, row, col); /* Query terminal dimensions */
|
||||
|
||||
/* Define color pallete foreground-background color pairs */
|
||||
/* PairID#, Foreground Color, Background Color */
|
||||
/* Foreground color == Background color ==> solid color block */
|
||||
init_pair(1, COLOR_BLACK, COLOR_BLACK); /* Black text on Black background */
|
||||
init_pair(2, COLOR_RED, COLOR_BLACK); /* Red text on Black background */
|
||||
init_pair(3, COLOR_WHITE, COLOR_BLUE); /* White text on Blue background */
|
||||
init_pair(4, COLOR_YELLOW, COLOR_YELLOW); /* Yellow text on Yellow background */
|
||||
|
||||
|
||||
for (c1 =(unsigned)0; c1 < row; c1++)
|
||||
{
|
||||
printw ("\n");
|
||||
for (c2=(unsigned)0; c2 < col; c2++)
|
||||
{
|
||||
if (maze==NULL)
|
||||
{
|
||||
break;
|
||||
}
|
||||
printw("%c", maze[c1][c2]);
|
||||
}
|
||||
}
|
||||
|
||||
/* free the memory */
|
||||
for(y = (unsigned)0; y < row; y++)
|
||||
{
|
||||
free(maze[y]);
|
||||
maze[y] = NULL;
|
||||
}
|
||||
if(maze != NULL){
|
||||
free(maze);
|
||||
}
|
||||
printw("\n");
|
||||
return 0;
|
||||
}
|
@ -0,0 +1,285 @@
|
||||
#include <ncurses.h>
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <stdint.h>
|
||||
#include <string.h>
|
||||
#include <unistd.h>
|
||||
|
||||
|
||||
/* */
|
||||
/* LDRA_EXCLUDE RULE VIOLATION <justification start> FALSE POSITIVE -- why <justification end> */
|
||||
|
||||
int main(int argc, char *argv[])
|
||||
{
|
||||
FILE* mazeFile; /* Maze Input File */
|
||||
unsigned int row = 0; /* Holds number of rows for input from file */
|
||||
unsigned int col = 0; /* Holds number of cols for input from file */
|
||||
unsigned int pcol = 0;
|
||||
unsigned int prow = 0;
|
||||
char shold[2000]; /* Holds lines for input from file */
|
||||
unsigned int l = 0;
|
||||
unsigned int k = 0;
|
||||
char **maze; /* array for maze */
|
||||
unsigned int c1 = 0;
|
||||
unsigned int c2 = 0;
|
||||
unsigned int i = 0;
|
||||
unsigned int j = 0;
|
||||
unsigned int x = 0;
|
||||
unsigned int y = 0;
|
||||
int deltaRow, deltaCol; /* Change in row-column position based on key input */
|
||||
unsigned int score = 0;
|
||||
|
||||
|
||||
WINDOW* W = 0; /* Curses Window handle */
|
||||
int rowLimit,colLimit; /* Num of rows and columns in terminal window */
|
||||
/* Initialize ncurses -- you will want to use the following settings */
|
||||
|
||||
|
||||
|
||||
/* check to see if argument */
|
||||
if (argc != 2)
|
||||
{
|
||||
/*printw("An error has ocurred no input file was given.\n");*/
|
||||
return 1;
|
||||
}
|
||||
mazeFile = fopen(argv[1], "r");
|
||||
if (mazeFile == NULL)
|
||||
{
|
||||
/*printw("Error in opening the file.\n");*/
|
||||
return 1;
|
||||
}
|
||||
fgets(shold, 2, mazeFile);
|
||||
while ( feof(mazeFile) == 0 )
|
||||
{
|
||||
|
||||
|
||||
switch(shold[0])
|
||||
{
|
||||
case 'M':
|
||||
/* read array values, dynamically allocate array */
|
||||
fscanf(mazeFile, "%3u %3u", &row, &col);
|
||||
maze = (char**)malloc(row * 8); /* */
|
||||
for(i = (unsigned) 0; i < row; i++)
|
||||
{
|
||||
if (maze==NULL)
|
||||
{
|
||||
break;
|
||||
}
|
||||
maze[i] = malloc(col * 8);
|
||||
}
|
||||
for (x = (unsigned) 0; x < row; x++)
|
||||
{
|
||||
for (j = (unsigned) 0; j < col; j++)
|
||||
{
|
||||
fscanf(mazeFile, "%c", &maze[x][j]);
|
||||
if (maze[x][j] == '\n')
|
||||
{
|
||||
fscanf(mazeFile, "%c", &maze[x][j]);
|
||||
}
|
||||
}
|
||||
}
|
||||
break;
|
||||
case 'P':
|
||||
fscanf(mazeFile, "%3u %3u", &k, &l);
|
||||
/* check maze bounds */
|
||||
if(k>= row || l>= col){
|
||||
break;
|
||||
}
|
||||
maze[k][l] = 'P';
|
||||
break;
|
||||
|
||||
case 'Z':
|
||||
fscanf(mazeFile, "%3u %3u", &k, &l);
|
||||
/* check maze bounds */
|
||||
if(k>= row || l>= col){
|
||||
break;
|
||||
}
|
||||
maze[k][l] = 'Z';
|
||||
break;
|
||||
|
||||
case 'T':
|
||||
fscanf(mazeFile, "%3u %3u", &k, &l);
|
||||
/* check maze bounds */
|
||||
if(k>= row || l>= col){
|
||||
break;
|
||||
}
|
||||
maze[k][l] = 'T';
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
};
|
||||
fgets(shold, 2, mazeFile);
|
||||
if (feof(mazeFile) != 0)
|
||||
{
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
fclose(mazeFile);
|
||||
/* LDRA_EXCLUDE 170 S <justification start> FALSE POSITIVE -- why <justification end> */
|
||||
W = initscr(); /* Determine terminal type and initialize curses data structures */
|
||||
start_color(); /* Enable use of color with ncurses */
|
||||
cbreak(); /* Disable line buffering and make char inputs immediately accessible to program */
|
||||
noecho(); /* Disable echo printing of chars type by user */
|
||||
if (W == NULL)
|
||||
{
|
||||
return 0;
|
||||
}
|
||||
nodelay(W, true); /* Make getch( ) a non-blocking call */
|
||||
keypad(W, true); /* Enable keypad and arrow keys */
|
||||
curs_set(0); /* Set cursor to be invisible - 0 */
|
||||
getmaxyx(W, rowLimit, colLimit); /* Query terminal dimensions */
|
||||
|
||||
/* Define color pallete foreground-background color pairs */
|
||||
/* PairID#, Foreground Color, Background Color */
|
||||
/* Foreground color == Background color ==> solid color block */
|
||||
init_pair(1, COLOR_BLACK, COLOR_BLACK); /* Black text on Black background */
|
||||
init_pair(2, COLOR_GREEN, COLOR_GREEN); /* Red text on Black background */
|
||||
init_pair(3, COLOR_WHITE, COLOR_WHITE); /* White text on Blue background */
|
||||
init_pair(4, COLOR_RED, COLOR_RED); /* Yellow text on Yellow background */
|
||||
init_pair(5, COLOR_YELLOW, COLOR_YELLOW); /* Yellow text on Yellow background */
|
||||
|
||||
|
||||
for (c1 =(unsigned)0; c1 < row; c1++)
|
||||
{
|
||||
/*printw ("\n");*/
|
||||
for (c2=(unsigned)0; c2 < col; c2++)
|
||||
{
|
||||
if (maze==NULL)
|
||||
{
|
||||
break;
|
||||
}
|
||||
switch(maze[c1][c2])
|
||||
{
|
||||
case '#':
|
||||
/* read array values, dynamically allocate array*/
|
||||
attron(COLOR_PAIR(2));
|
||||
wmove(W, c1,c2);
|
||||
waddch(W,'#');
|
||||
attroff(COLOR_PAIR(2));
|
||||
break;
|
||||
case ' ':
|
||||
wmove(W, c1,c2);
|
||||
waddch(W,' ');
|
||||
break;
|
||||
case 'P':
|
||||
wmove(W, c1,c2);
|
||||
waddch(W,'P');
|
||||
prow=c1;
|
||||
pcol=c2;
|
||||
break;
|
||||
case 'Z':
|
||||
attron(COLOR_PAIR(4));
|
||||
wmove(W, c1,c2);
|
||||
waddch(W,'Z');
|
||||
attroff(COLOR_PAIR(4));
|
||||
break;
|
||||
case 'T':
|
||||
attron(COLOR_PAIR(5));
|
||||
wmove(W, c1,c2);
|
||||
waddch(W,'T');
|
||||
attroff(COLOR_PAIR(5));
|
||||
break;
|
||||
|
||||
default:
|
||||
break;
|
||||
};
|
||||
/*printw("%c", maze[c1][c2]);*/
|
||||
}
|
||||
}
|
||||
|
||||
deltaRow =0;
|
||||
deltaCol =0;
|
||||
|
||||
|
||||
|
||||
do{
|
||||
|
||||
int kb = wgetch(W); /* Grab keypress */
|
||||
|
||||
/* Support player movement via WASD, IJKL, and arrow keys */
|
||||
if (kb == (int)'a' || kb == (int)'j' || kb == KEY_LEFT)
|
||||
{
|
||||
/* Move Left */
|
||||
deltaRow =0;
|
||||
deltaCol =-1;
|
||||
}
|
||||
else if (kb == (int)'d' || kb == (int)'l' || kb == KEY_RIGHT)
|
||||
{
|
||||
/* Move Right */
|
||||
deltaRow =0;
|
||||
deltaCol =1;
|
||||
}
|
||||
else if (kb == (int)'w' || kb == (int)'i' || kb == KEY_UP)
|
||||
{
|
||||
/* Move Up */
|
||||
deltaRow =-1;
|
||||
deltaCol =0;
|
||||
}
|
||||
else if (kb == (int)'s' || kb == (int)'k' || kb == KEY_DOWN)
|
||||
{
|
||||
/* Move Down */
|
||||
deltaRow =1;
|
||||
deltaCol =0;
|
||||
}
|
||||
else if (kb == (int)'q')
|
||||
{
|
||||
/* q to exit player movement loop */
|
||||
break;
|
||||
};
|
||||
|
||||
attron(COLOR_PAIR(1));
|
||||
wmove(W,prow,pcol);
|
||||
waddch(W, 'P');
|
||||
attroff(COLOR_PAIR(1));
|
||||
|
||||
if(((prow + (unsigned int)deltaRow) > row) || ((pcol + (unsigned int)deltaCol) > col)){
|
||||
deltaRow = 0;
|
||||
deltaCol = 0;
|
||||
}
|
||||
if(maze[((int)prow + deltaRow)][((int)pcol + deltaCol)] == '#'){
|
||||
prow = prow;
|
||||
pcol = pcol;
|
||||
}
|
||||
else{
|
||||
prow = prow + (unsigned int)deltaRow;
|
||||
pcol = pcol + (unsigned int)deltaCol;
|
||||
}
|
||||
if(maze[prow][pcol] == 'T')
|
||||
{
|
||||
maze[prow][pcol] = ' ';
|
||||
score++;
|
||||
}
|
||||
if(maze[prow][pcol] == 'Z')
|
||||
{
|
||||
break;
|
||||
}
|
||||
|
||||
attron(COLOR_PAIR(3));
|
||||
wmove(W,prow,pcol);
|
||||
waddch(W, 'P');
|
||||
attroff(COLOR_PAIR(3));
|
||||
|
||||
deltaRow =0;
|
||||
deltaCol =0;
|
||||
|
||||
wrefresh(W); /* Refresh ncurses window */
|
||||
|
||||
} while (true); /* Repeat until user selects q to quit */
|
||||
|
||||
/* free the memory */
|
||||
for(y = (unsigned)0; y < row; y++)
|
||||
{
|
||||
free(maze[y]);
|
||||
maze[y] = NULL;
|
||||
}
|
||||
if(maze != NULL){
|
||||
free(maze);
|
||||
}
|
||||
/*printw("\n");*/
|
||||
endwin();
|
||||
printf("Score = %u\n", score);
|
||||
return 0;
|
||||
}
|
||||
|
@ -0,0 +1,112 @@
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <stdint.h>
|
||||
#include <string.h>
|
||||
|
||||
|
||||
|
||||
|
||||
int main(int argc, char *argv[])
|
||||
{
|
||||
FILE* mazeFile; /* Maze Input File */
|
||||
char chold = NULL; /* Holds character for line input from file */
|
||||
unsigned int row = 0; /* Holds number of rows for input from file */
|
||||
unsigned int col = 0; /* Holds number of cols for input from file */
|
||||
char shold[2000]; /* Holds lines for input from file */
|
||||
unsigned int l = 0;
|
||||
unsigned int k = 0;
|
||||
char **maze; // global array for maze
|
||||
unsigned int c1 = 0;
|
||||
unsigned int c2 = 0;
|
||||
|
||||
// check to see if
|
||||
if (argc != 2)
|
||||
{
|
||||
printf("An error has ocurred no input file was given.\n");
|
||||
return 1;
|
||||
}
|
||||
mazeFile = fopen(argv[1], "r");
|
||||
if (mazeFile == NULL)
|
||||
{
|
||||
printf("Error in opening the file.\n");
|
||||
return 1;
|
||||
}
|
||||
fgets(shold, 2, mazeFile);
|
||||
while ( feof(mazeFile) == 0 )
|
||||
{
|
||||
|
||||
switch(shold[0])
|
||||
{
|
||||
case 'M':
|
||||
fscanf(mazeFile, "%3d %3d", &row, &col);
|
||||
// dynamic allocation of array for maze
|
||||
unsigned int i = 0, j = 0;
|
||||
maze = malloc(row * sizeof(unsigned int *));
|
||||
for(i = 0; i < row; i++)
|
||||
{
|
||||
if (maze==NULL)
|
||||
{
|
||||
break;
|
||||
}
|
||||
maze[i] = malloc(col * sizeof(unsigned int *));
|
||||
}
|
||||
for (i = 0; i < row; i++)
|
||||
{
|
||||
for (j = 0; j < col; j++)
|
||||
{
|
||||
fscanf(mazeFile, "%c", &maze[i][j]);
|
||||
if (maze[i][j] == '\n')
|
||||
{
|
||||
fscanf(mazeFile, "%c", &maze[i][j]);
|
||||
}
|
||||
}
|
||||
}
|
||||
break;
|
||||
case 'P':
|
||||
fscanf(mazeFile, "%3d %3d", &k, &l);
|
||||
maze[k][l] = 'P';
|
||||
break;
|
||||
|
||||
case 'Z':
|
||||
fscanf(mazeFile, "%3d %3d", &k, &l);
|
||||
maze[k][l] = 'Z';
|
||||
break;
|
||||
|
||||
case 'T':
|
||||
fscanf(mazeFile, "%3d %3d", &k, &l);
|
||||
maze[k][l] = 'T';
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
};
|
||||
fgets(shold, 2, mazeFile);
|
||||
if (feof(mazeFile) != 0)
|
||||
{
|
||||
break;
|
||||
}
|
||||
}
|
||||
fclose(mazeFile);
|
||||
for (c1 =0; c1 < row; c1++)
|
||||
{
|
||||
printf ("\n");
|
||||
for (c2=0; c2 < col; c2++)
|
||||
{
|
||||
if (maze==NULL)
|
||||
{
|
||||
break;
|
||||
}
|
||||
printf("%c", maze[c1][c2]);
|
||||
}
|
||||
}
|
||||
|
||||
//free the memory
|
||||
unsigned int i = 0;
|
||||
for(i = 0; i < row; i++)
|
||||
{
|
||||
free(maze[i]);
|
||||
}
|
||||
free(maze);
|
||||
printf("\n");
|
||||
|
||||
return 0;
|
||||
}
|
@ -0,0 +1,287 @@
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <stdint.h>
|
||||
#include <string.h>
|
||||
#include <unistd.h>
|
||||
#include <ncurses.h>
|
||||
|
||||
WINDOW *initscr(void);
|
||||
int start_color(void);
|
||||
int cbreak(void);
|
||||
int noecho(void);
|
||||
int nodelay(WINDOW *win, bool bf);
|
||||
int keypad(WINDOW *win, bool bf);
|
||||
int curs_set(int visibility);
|
||||
/*void getmaxyx(WINDOW *win, int y, int x); */
|
||||
int init_pair(short pair, short f, short b);
|
||||
/*nothing*/
|
||||
|
||||
int main(int argc, char *argv[])
|
||||
{
|
||||
FILE* mazeFile; /* Maze Input File */
|
||||
unsigned int row = 0; /* Holds number of rows for input from file */
|
||||
unsigned int col = 0; /* Holds number of cols for input from file */
|
||||
unsigned int pcol = 0;
|
||||
unsigned int prow = 0;
|
||||
char shold[2000]; /* Holds lines for input from file */
|
||||
unsigned int l = 0;
|
||||
unsigned int k = 0;
|
||||
char **maze; /* array for maze */
|
||||
unsigned int c1 = 0;
|
||||
unsigned int c2 = 0;
|
||||
unsigned int i = 0;
|
||||
unsigned int j = 0;
|
||||
unsigned int x = 0;
|
||||
unsigned int y = 0;
|
||||
int deltaRow, deltaCol; /* Change in row-column position based on key input */
|
||||
unsigned int score = 0;
|
||||
|
||||
|
||||
WINDOW* W = 0; /* Curses Window handle */
|
||||
int rowLimit,colLimit; /* Num of rows and columns in terminal window */
|
||||
/* Initialize ncurses -- you will want to use the following settings */
|
||||
|
||||
|
||||
|
||||
/* check to see if argument */
|
||||
if (argc != 2)
|
||||
{
|
||||
/*printw("An error has ocurred no input file was given.\n");*/
|
||||
return 1;
|
||||
}
|
||||
mazeFile = fopen(argv[1], "r");
|
||||
if (mazeFile == NULL)
|
||||
{
|
||||
/*printw("Error in opening the file.\n");*/
|
||||
return 1;
|
||||
}
|
||||
fgets(shold, 2, mazeFile);
|
||||
while ( feof(mazeFile) == 0 )
|
||||
{
|
||||
|
||||
|
||||
switch(shold[0])
|
||||
{
|
||||
case 'M':
|
||||
/* read array values, dynamically allocate array */
|
||||
fscanf(mazeFile, "%3u %3u", &row, &col);
|
||||
maze = (char**)malloc(row * 8);
|
||||
for(i = (unsigned) 0; i < row; i++)
|
||||
{
|
||||
if (maze==NULL)
|
||||
{
|
||||
break;
|
||||
}
|
||||
maze[i] = malloc(col * 8);
|
||||
}
|
||||
for (x = (unsigned) 0; x < row; x++)
|
||||
{
|
||||
for (j = (unsigned) 0; j < col; j++)
|
||||
{
|
||||
fscanf(mazeFile, "%c", &maze[x][j]);
|
||||
if (maze[x][j] == '\n')
|
||||
{
|
||||
fscanf(mazeFile, "%c", &maze[x][j]);
|
||||
}
|
||||
}
|
||||
}
|
||||
break;
|
||||
case 'P':
|
||||
fscanf(mazeFile, "%3u %3u", &k, &l);
|
||||
/* check maze bounds */
|
||||
if(k>= row || l>= col){
|
||||
break;
|
||||
}
|
||||
maze[k][l] = 'P';
|
||||
break;
|
||||
|
||||
case 'Z':
|
||||
fscanf(mazeFile, "%3u %3u", &k, &l);
|
||||
/* check maze bounds */
|
||||
if(k>= row || l>= col){
|
||||
break;
|
||||
}
|
||||
maze[k][l] = 'Z';
|
||||
break;
|
||||
|
||||
case 'T':
|
||||
fscanf(mazeFile, "%3u %3u", &k, &l);
|
||||
/* check maze bounds */
|
||||
if(k>= row || l>= col){
|
||||
break;
|
||||
}
|
||||
maze[k][l] = 'T';
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
};
|
||||
fgets(shold, 2, mazeFile);
|
||||
if (feof(mazeFile) != 0)
|
||||
{
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
fclose(mazeFile);
|
||||
W = initscr(); /* Determine terminal type and initialize curses data structures */
|
||||
start_color(); /* Enable use of color with ncurses */
|
||||
cbreak(); /* Disable line buffering and make char inputs immediately accessible to program */
|
||||
noecho(); /* Disable echo printing of chars type by user */
|
||||
nodelay(W, true); /* Make getch( ) a non-blocking call */
|
||||
keypad(W, true); /* Enable keypad and arrow keys */
|
||||
curs_set(0); /* Set cursor to be invisible - 0 */
|
||||
getmaxyx(W, rowLimit, colLimit); /* Query terminal dimensions */
|
||||
|
||||
/* Define color pallete foreground-background color pairs */
|
||||
/* PairID#, Foreground Color, Background Color */
|
||||
/* Foreground color == Background color ==> solid color block */
|
||||
init_pair(1, COLOR_BLACK, COLOR_BLACK); /* Black text on Black background */
|
||||
init_pair(2, COLOR_GREEN, COLOR_GREEN); /* Red text on Black background */
|
||||
init_pair(3, COLOR_WHITE, COLOR_WHITE); /* White text on Blue background */
|
||||
init_pair(4, COLOR_RED, COLOR_RED); /* Yellow text on Yellow background */
|
||||
init_pair(5, COLOR_YELLOW, COLOR_YELLOW); /* Yellow text on Yellow background */
|
||||
|
||||
|
||||
for (c1 =(unsigned)0; c1 < row; c1++)
|
||||
{
|
||||
/*printw ("\n");*/
|
||||
for (c2=(unsigned)0; c2 < col; c2++)
|
||||
{
|
||||
if (maze==NULL)
|
||||
{
|
||||
break;
|
||||
}
|
||||
switch(maze[c1][c2])
|
||||
{
|
||||
case '#':
|
||||
/* read array values, dynamically allocate array*/
|
||||
attron(COLOR_PAIR(2));
|
||||
wmove(W, c1,c2);
|
||||
waddch(W,'#');
|
||||
attroff(COLOR_PAIR(2));
|
||||
break;
|
||||
case ' ':
|
||||
wmove(W, c1,c2);
|
||||
waddch(W,' ');
|
||||
break;
|
||||
case 'P':
|
||||
wmove(W, c1,c2);
|
||||
waddch(W,'P');
|
||||
prow=c1;
|
||||
pcol=c2;
|
||||
break;
|
||||
case 'Z':
|
||||
attron(COLOR_PAIR(4));
|
||||
wmove(W, c1,c2);
|
||||
waddch(W,'Z');
|
||||
attroff(COLOR_PAIR(4));
|
||||
break;
|
||||
case 'T':
|
||||
attron(COLOR_PAIR(5));
|
||||
wmove(W, c1,c2);
|
||||
waddch(W,'T');
|
||||
attroff(COLOR_PAIR(5));
|
||||
break;
|
||||
|
||||
default:
|
||||
break;
|
||||
};
|
||||
/*printw("%c", maze[c1][c2]);*/
|
||||
}
|
||||
}
|
||||
|
||||
deltaRow =0;
|
||||
deltaCol =0;
|
||||
|
||||
|
||||
|
||||
do{
|
||||
|
||||
int kb = wgetch(W); /* Grab keypress */
|
||||
|
||||
/* Support player movement via WASD, IJKL, and arrow keys */
|
||||
if (kb == 'a' || kb == 'j' || kb == KEY_LEFT)
|
||||
{
|
||||
/* Move Left */
|
||||
deltaRow =0;
|
||||
deltaCol =-1;
|
||||
}
|
||||
else if (kb == 'd' || kb == 'l' || kb == KEY_RIGHT)
|
||||
{
|
||||
/* Move Right */
|
||||
deltaRow =0;
|
||||
deltaCol =1;
|
||||
}
|
||||
else if (kb == 'w' || kb == 'i' || kb == KEY_UP)
|
||||
{
|
||||
/* Move Up */
|
||||
deltaRow =-1;
|
||||
deltaCol =0;
|
||||
}
|
||||
else if (kb == 's' || kb == 'k' || kb == KEY_DOWN)
|
||||
{
|
||||
/* Move Down */
|
||||
deltaRow =1;
|
||||
deltaCol =0;
|
||||
}
|
||||
else if (kb == 'q')
|
||||
{
|
||||
/* q to exit player movement loop */
|
||||
break;
|
||||
};
|
||||
|
||||
attron(COLOR_PAIR(1));
|
||||
wmove(W,prow,pcol);
|
||||
waddch(W, 'P');
|
||||
attroff(COLOR_PAIR(1));
|
||||
|
||||
if((prow + deltaRow > row) || (pcol + deltaCol > col)){
|
||||
deltaRow = 0;
|
||||
deltaCol = 0;
|
||||
}
|
||||
if(maze[prow + deltaRow][pcol + deltaCol] == '#'){
|
||||
prow = prow;
|
||||
pcol = pcol;
|
||||
}
|
||||
else{
|
||||
prow = prow + deltaRow;
|
||||
pcol = pcol + deltaCol;
|
||||
}
|
||||
if(maze[prow][pcol] == 'T')
|
||||
{
|
||||
maze[prow][pcol] = ' ';
|
||||
score++;
|
||||
}
|
||||
if(maze[prow][pcol] == 'Z')
|
||||
{
|
||||
break;
|
||||
}
|
||||
|
||||
attron(COLOR_PAIR(3));
|
||||
wmove(W,prow,pcol);
|
||||
waddch(W, 'P');
|
||||
attroff(COLOR_PAIR(3));
|
||||
|
||||
deltaRow =0;
|
||||
deltaCol =0;
|
||||
|
||||
wrefresh(W); /* Refresh ncurses window */
|
||||
|
||||
} while (true); /* Repeat until user selects q to quit */
|
||||
|
||||
/* free the memory */
|
||||
for(y = (unsigned)0; y < row; y++)
|
||||
{
|
||||
free(maze[y]);
|
||||
maze[y] = NULL;
|
||||
}
|
||||
if(maze != NULL){
|
||||
free(maze);
|
||||
}
|
||||
/*printw("\n");*/
|
||||
endwin();
|
||||
printf("Score = %u\n", score);
|
||||
return 0;
|
||||
}
|
||||
|
@ -0,0 +1,382 @@
|
||||
#include <ncurses.h>
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <stdint.h>
|
||||
#include <string.h>
|
||||
#include <unistd.h>
|
||||
|
||||
extern WINDOW* initscr(void);
|
||||
extern int start_color(void);
|
||||
|
||||
/* LDRA_EXCLUDE RULE VIOLATION <justification start> FALSE POSITIVE -- why <justification end> */
|
||||
|
||||
int main(int argc, char *argv[])
|
||||
{
|
||||
FILE* mazeFile; /* Maze Input File */
|
||||
unsigned int row = 0; /* Holds number of rows for input from file */
|
||||
unsigned int col = 0; /* Holds number of cols for input from file */
|
||||
unsigned int pcol = 0;
|
||||
unsigned int prow = 0;
|
||||
char shold[2000]; /* Holds lines for input from file */
|
||||
unsigned int l = 0;
|
||||
unsigned int k = 0;
|
||||
char **maze; /* array for maze */
|
||||
unsigned int c1 = 0;
|
||||
unsigned int c2 = 0;
|
||||
unsigned int i = 0;
|
||||
unsigned int j = 0;
|
||||
unsigned int x = 0;
|
||||
unsigned int y = 0;
|
||||
int deltaRow, deltaCol; /* Change in row-column position based on key input */
|
||||
unsigned int score = 0;
|
||||
|
||||
|
||||
WINDOW* W = 0; /* Curses Window handle */
|
||||
unsigned int rowLimit,colLimit; /* Num of rows and columns in terminal window */
|
||||
/* Initialize ncurses -- you will want to use the following settings */
|
||||
|
||||
|
||||
|
||||
/* check to see if argument */
|
||||
if (argc != 2)
|
||||
{
|
||||
/*printw("An error has ocurred no input file was given.\n");*/
|
||||
return 1;
|
||||
}
|
||||
mazeFile = fopen(argv[1], "r");
|
||||
if (mazeFile == NULL)
|
||||
{
|
||||
/*printw("Error in opening the file.\n");*/
|
||||
return 1;
|
||||
}
|
||||
fgets(shold, 2, mazeFile);
|
||||
while ( feof(mazeFile) == 0 )
|
||||
{
|
||||
|
||||
|
||||
switch(shold[0])
|
||||
{
|
||||
case 'M':
|
||||
/* read array values, dynamically allocate array */
|
||||
fscanf(mazeFile, "%3u %3u", &row, &col);
|
||||
maze = (char**)malloc(row * 8); /* */
|
||||
for(i = (unsigned) 0; i < row; i++)
|
||||
{
|
||||
if (maze==NULL)
|
||||
{
|
||||
break;
|
||||
}
|
||||
maze[i] = malloc(col * 8);
|
||||
}
|
||||
for (x = (unsigned) 0; x < row; x++)
|
||||
{
|
||||
for (j = (unsigned) 0; j < col; j++)
|
||||
{
|
||||
fscanf(mazeFile, "%c", &maze[x][j]);
|
||||
if (maze[x][j] == '\n')
|
||||
{
|
||||
fscanf(mazeFile, "%c", &maze[x][j]);
|
||||
}
|
||||
}
|
||||
}
|
||||
break;
|
||||
case 'P':
|
||||
fscanf(mazeFile, "%3u %3u", &k, &l);
|
||||
/* check maze bounds */
|
||||
if(k>= row || l>= col){
|
||||
break;
|
||||
}
|
||||
maze[k][l] = 'P';
|
||||
break;
|
||||
|
||||
case 'Z':
|
||||
fscanf(mazeFile, "%3u %3u", &k, &l);
|
||||
/* check maze bounds */
|
||||
if(k>= row || l>= col){
|
||||
break;
|
||||
}
|
||||
maze[k][l] = 'Z';
|
||||
break;
|
||||
|
||||
case 'T':
|
||||
fscanf(mazeFile, "%3u %3u", &k, &l);
|
||||
/* check maze bounds */
|
||||
if(k>= row || l>= col){
|
||||
break;
|
||||
}
|
||||
maze[k][l] = 'T';
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
};
|
||||
fgets(shold, 2, mazeFile);
|
||||
if (feof(mazeFile) != 0)
|
||||
{
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
rowLimit = row;
|
||||
colLimit = col;
|
||||
fclose(mazeFile);
|
||||
/* LDRA_EXCLUDE 170 S <justification start> FALSE POSITIVE -- needed for ncurses <justification end> */
|
||||
W = initscr(); /* Determine terminal type and initialize curses data structures */
|
||||
/* LDRA_EXCLUDE 170 S <justification start> FALSE POSITIVE -- needed for ncurses <justification end> */
|
||||
start_color(); /* Enable use of color with ncurses */
|
||||
/* LDRA_EXCLUDE 170 S <justification start> FALSE POSITIVE -- needed for ncurses <justification end> */
|
||||
/* LDRA_EXCLUDE 496 S <justification start> FALSE POSITIVE -- needed for ncurses <justification end> */
|
||||
cbreak(); /* Disable line buffering and make char inputs immediately accessible to program */
|
||||
/* LDRA_EXCLUDE 170 S <justification start> FALSE POSITIVE -- needed for ncurses <justification end> */
|
||||
/* LDRA_EXCLUDE 496 S <justification start> FALSE POSITIVE -- needed for ncurses <justification end> */
|
||||
noecho(); /* Disable echo printing of chars type by user */
|
||||
if (W == NULL)
|
||||
{
|
||||
return 0;
|
||||
}
|
||||
/* LDRA_EXCLUDE 170 S <justification start> FALSE POSITIVE -- needed for ncurses <justification end> */
|
||||
/* LDRA_EXCLUDE 496 S <justification start> FALSE POSITIVE -- needed for ncurses <justification end> */
|
||||
nodelay(W, true); /* Make getch( ) a non-blocking call */
|
||||
/* LDRA_EXCLUDE 170 S <justification start> FALSE POSITIVE -- needed for ncurses <justification end> */
|
||||
/* LDRA_EXCLUDE 496 S <justification start> FALSE POSITIVE -- needed for ncurses <justification end> */
|
||||
keypad(W, true); /* Enable keypad and arrow keys */
|
||||
/* LDRA_EXCLUDE 170 S <justification start> FALSE POSITIVE -- needed for ncurses <justification end> */
|
||||
/* LDRA_EXCLUDE 496 S <justification start> FALSE POSITIVE -- needed for ncurses <justification end> */
|
||||
curs_set(0); /* Set cursor to be invisible - 0 */
|
||||
/* LDRA_EXCLUDE 170 S <justification start> FALSE POSITIVE -- needed for ncurses <justification end> */
|
||||
/* LDRA_EXCLUDE 496 S <justification start> FALSE POSITIVE -- needed for ncurses <justification end> */
|
||||
getmaxyx(W, rowLimit, colLimit); /* Query terminal dimensions */
|
||||
|
||||
/* Define color pallete foreground-background color pairs */
|
||||
/* PairID#, Foreground Color, Background Color */
|
||||
/* Foreground color == Background color ==> solid color block */
|
||||
/* LDRA_EXCLUDE 496 S <justification start> FALSE POSITIVE -- needed for ncurses <justification end> */
|
||||
init_pair(1, COLOR_BLACK, COLOR_BLACK); /* Black text on Black background */
|
||||
/* LDRA_EXCLUDE 170 S <justification start> FALSE POSITIVE -- needed for ncurses <justification end> */
|
||||
/* LDRA_EXCLUDE 496 S <justification start> FALSE POSITIVE -- needed for ncurses <justification end> */
|
||||
init_pair(2, COLOR_GREEN, COLOR_GREEN); /* Red text on Black background */
|
||||
/* LDRA_EXCLUDE 170 S <justification start> FALSE POSITIVE -- needed for ncurses <justification end> */
|
||||
/* LDRA_EXCLUDE 496 S <justification start> FALSE POSITIVE -- needed for ncurses <justification end> */
|
||||
init_pair(3, COLOR_WHITE, COLOR_WHITE); /* White text on Blue background */
|
||||
/* LDRA_EXCLUDE 170 S <justification start> FALSE POSITIVE -- needed for ncurses <justification end> */
|
||||
/* LDRA_EXCLUDE 496 S <justification start> FALSE POSITIVE -- needed for ncurses <justification end> */
|
||||
init_pair(4, COLOR_RED, COLOR_RED); /* Yellow text on Yellow background */
|
||||
/* LDRA_EXCLUDE 170 S <justification start> FALSE POSITIVE -- needed for ncurses <justification end> */
|
||||
/* LDRA_EXCLUDE 496 S <justification start> FALSE POSITIVE -- needed for ncurses <justification end> */
|
||||
init_pair(5, COLOR_YELLOW, COLOR_YELLOW); /* Yellow text on Yellow background */
|
||||
|
||||
|
||||
for (c1 =(unsigned)0; c1 < row; c1++)
|
||||
{
|
||||
/*printw ("\n");*/
|
||||
for (c2=(unsigned)0; c2 < col; c2++)
|
||||
{
|
||||
if (maze==NULL)
|
||||
{
|
||||
break;
|
||||
}
|
||||
switch(maze[c1][c2])
|
||||
{
|
||||
case '#':
|
||||
/* read array values, dynamically allocate array*/
|
||||
/* LDRA_EXCLUDE 170 S <justification start> FALSE POSITIVE -- needed for ncurses <justification end> */
|
||||
/* LDRA_EXCLUDE 170 S <justification start> FALSE POSITIVE -- needed for ncurses <justification end> */
|
||||
/* LDRA_EXCLUDE 496 S <justification start> FALSE POSITIVE -- needed for ncurses <justification end> */
|
||||
/* LDRA_EXCLUDE 496 S <justification start> FALSE POSITIVE -- needed for ncurses <justification end> */
|
||||
attron(COLOR_PAIR(2));
|
||||
/* LDRA_EXCLUDE 170 S <justification start> FALSE POSITIVE -- needed for ncurses <justification end> */
|
||||
/* LDRA_EXCLUDE 496 S <justification start> FALSE POSITIVE -- needed for ncurses <justification end> */
|
||||
wmove(W, c1,c2);
|
||||
/* LDRA_EXCLUDE 170 S <justification start> FALSE POSITIVE -- needed for ncurses <justification end> */
|
||||
/* LDRA_EXCLUDE 496 S <justification start> FALSE POSITIVE -- needed for ncurses <justification end> */
|
||||
waddch(W,'#');
|
||||
/* LDRA_EXCLUDE 170 S <justification start> FALSE POSITIVE -- needed for ncurses <justification end> */
|
||||
/* LDRA_EXCLUDE 170 S <justification start> FALSE POSITIVE -- needed for ncurses <justification end> */
|
||||
/* LDRA_EXCLUDE 496 S <justification start> FALSE POSITIVE -- needed for ncurses <justification end> */
|
||||
/* LDRA_EXCLUDE 496 S <justification start> FALSE POSITIVE -- needed for ncurses <justification end> */
|
||||
attroff(COLOR_PAIR(2));
|
||||
break;
|
||||
case ' ':
|
||||
/* LDRA_EXCLUDE 170 S <justification start> FALSE POSITIVE -- needed for ncurses <justification end> */
|
||||
/* LDRA_EXCLUDE 496 S <justification start> FALSE POSITIVE -- needed for ncurses <justification end> */
|
||||
wmove(W, c1,c2);
|
||||
/* LDRA_EXCLUDE 170 S <justification start> FALSE POSITIVE -- needed for ncurses <justification end> */
|
||||
/* LDRA_EXCLUDE 496 S <justification start> FALSE POSITIVE -- needed for ncurses <justification end> */
|
||||
waddch(W,' ');
|
||||
break;
|
||||
case 'P':
|
||||
/* LDRA_EXCLUDE 170 S <justification start> FALSE POSITIVE -- needed for ncurses <justification end> */
|
||||
/* LDRA_EXCLUDE 496 S <justification start> FALSE POSITIVE -- needed for ncurses <justification end> */
|
||||
wmove(W, c1,c2);
|
||||
/* LDRA_EXCLUDE 170 S <justification start> FALSE POSITIVE -- needed for ncurses <justification end> */
|
||||
/* LDRA_EXCLUDE 496 S <justification start> FALSE POSITIVE -- needed for ncurses <justification end> */
|
||||
waddch(W,'P');
|
||||
prow=c1;
|
||||
pcol=c2;
|
||||
break;
|
||||
case 'Z':
|
||||
/* LDRA_EXCLUDE 170 S <justification start> FALSE POSITIVE -- needed for ncurses <justification end> */
|
||||
/* LDRA_EXCLUDE 170 S <justification start> FALSE POSITIVE -- needed for ncurses <justification end> */
|
||||
/* LDRA_EXCLUDE 496 S <justification start> FALSE POSITIVE -- needed for ncurses <justification end> */
|
||||
/* LDRA_EXCLUDE 496 S <justification start> FALSE POSITIVE -- needed for ncurses <justification end> */
|
||||
attron(COLOR_PAIR(4));
|
||||
/* LDRA_EXCLUDE 170 S <justification start> FALSE POSITIVE -- needed for ncurses <justification end> */
|
||||
/* LDRA_EXCLUDE 496 S <justification start> FALSE POSITIVE -- needed for ncurses <justification end> */
|
||||
wmove(W, c1,c2);
|
||||
/* LDRA_EXCLUDE 170 S <justification start> FALSE POSITIVE -- needed for ncurses <justification end> */
|
||||
/* LDRA_EXCLUDE 496 S <justification start> FALSE POSITIVE -- needed for ncurses <justification end> */
|
||||
waddch(W,'Z');
|
||||
/* LDRA_EXCLUDE 170 S <justification start> FALSE POSITIVE -- needed for ncurses <justification end> */
|
||||
/* LDRA_EXCLUDE 170 S <justification start> FALSE POSITIVE -- needed for ncurses <justification end> */
|
||||
/* LDRA_EXCLUDE 496 S <justification start> FALSE POSITIVE -- needed for ncurses <justification end> */
|
||||
/* LDRA_EXCLUDE 496 S <justification start> FALSE POSITIVE -- needed for ncurses <justification end> */
|
||||
attroff(COLOR_PAIR(4));
|
||||
break;
|
||||
case 'T':
|
||||
/* LDRA_EXCLUDE 170 S <justification start> FALSE POSITIVE -- needed for ncurses <justification end> */
|
||||
/* LDRA_EXCLUDE 170 S <justification start> FALSE POSITIVE -- needed for ncurses <justification end> */
|
||||
/* LDRA_EXCLUDE 496 S <justification start> FALSE POSITIVE -- needed for ncurses <justification end> */
|
||||
/* LDRA_EXCLUDE 496 S <justification start> FALSE POSITIVE -- needed for ncurses <justification end> */
|
||||
attron(COLOR_PAIR(5));
|
||||
/* LDRA_EXCLUDE 170 S <justification start> FALSE POSITIVE -- needed for ncurses <justification end> */
|
||||
/* LDRA_EXCLUDE 496 S <justification start> FALSE POSITIVE -- needed for ncurses <justification end> */
|
||||
wmove(W, c1,c2);
|
||||
/* LDRA_EXCLUDE 170 S <justification start> FALSE POSITIVE -- needed for ncurses <justification end> */
|
||||
/* LDRA_EXCLUDE 496 S <justification start> FALSE POSITIVE -- needed for ncurses <justification end> */
|
||||
waddch(W,'T');
|
||||
/* LDRA_EXCLUDE 170 S <justification start> FALSE POSITIVE -- needed for ncurses <justification end> */
|
||||
/* LDRA_EXCLUDE 170 S <justification start> FALSE POSITIVE -- needed for ncurses <justification end> */
|
||||
/* LDRA_EXCLUDE 496 S <justification start> FALSE POSITIVE -- needed for ncurses <justification end> */
|
||||
/* LDRA_EXCLUDE 496 S <justification start> FALSE POSITIVE -- needed for ncurses <justification end> */
|
||||
attroff(COLOR_PAIR(5));
|
||||
break;
|
||||
|
||||
default:
|
||||
break;
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
deltaRow =0;
|
||||
deltaCol =0;
|
||||
|
||||
|
||||
|
||||
do{
|
||||
|
||||
/* LDRA_EXCLUDE 170 S <justification start> FALSE POSITIVE -- needed for ncurses <justification end> */
|
||||
/* LDRA_EXCLUDE 496 S <justification start> FALSE POSITIVE -- needed for ncurses <justification end> */
|
||||
int kb = wgetch(W); /* Grab keypress */
|
||||
|
||||
/* Support player movement via WASD, IJKL, and arrow keys */
|
||||
if (kb == (int)'a' || kb == (int)'j' || kb == KEY_LEFT)
|
||||
{
|
||||
/* Move Left */
|
||||
deltaRow =0;
|
||||
deltaCol =-1;
|
||||
}
|
||||
else if (kb == (int)'d' || kb == (int)'l' || kb == KEY_RIGHT)
|
||||
{
|
||||
/* Move Right */
|
||||
deltaRow =0;
|
||||
deltaCol =1;
|
||||
}
|
||||
else if (kb == (int)'w' || kb == (int)'i' || kb == KEY_UP)
|
||||
{
|
||||
/* Move Up */
|
||||
deltaRow =-1;
|
||||
deltaCol =0;
|
||||
}
|
||||
else if (kb == (int)'s' || kb == (int)'k' || kb == KEY_DOWN)
|
||||
{
|
||||
/* Move Down */
|
||||
deltaRow =1;
|
||||
deltaCol =0;
|
||||
}
|
||||
else if (kb == (int)'q')
|
||||
{
|
||||
/* q to exit player movement loop */
|
||||
break;
|
||||
};
|
||||
|
||||
/* LDRA_EXCLUDE 170 S <justification start> FALSE POSITIVE -- needed for ncurses <justification end> */
|
||||
/* LDRA_EXCLUDE 170 S <justification start> FALSE POSITIVE -- needed for ncurses <justification end> */
|
||||
/* LDRA_EXCLUDE 496 S <justification start> FALSE POSITIVE -- needed for ncurses <justification end> */
|
||||
/* LDRA_EXCLUDE 496 S <justification start> FALSE POSITIVE -- needed for ncurses <justification end> */
|
||||
attron(COLOR_PAIR(1));
|
||||
/* LDRA_EXCLUDE 170 S <justification start> FALSE POSITIVE -- needed for ncurses <justification end> */
|
||||
/* LDRA_EXCLUDE 496 S <justification start> FALSE POSITIVE -- needed for ncurses <justification end> */
|
||||
wmove(W,prow,pcol);
|
||||
/* LDRA_EXCLUDE 170 S <justification start> FALSE POSITIVE -- needed for ncurses <justification end> */
|
||||
/* LDRA_EXCLUDE 496 S <justification start> FALSE POSITIVE -- needed for ncurses <justification end> */
|
||||
waddch(W, 'P');
|
||||
/* LDRA_EXCLUDE 170 S <justification start> FALSE POSITIVE -- needed for ncurses <justification end> */
|
||||
/* LDRA_EXCLUDE 170 S <justification start> FALSE POSITIVE -- needed for ncurses <justification end> */
|
||||
/* LDRA_EXCLUDE 496 S <justification start> FALSE POSITIVE -- needed for ncurses <justification end> */
|
||||
/* LDRA_EXCLUDE 496 S <justification start> FALSE POSITIVE -- needed for ncurses <justification end> */
|
||||
attroff(COLOR_PAIR(1));
|
||||
|
||||
if(((prow + (unsigned int)deltaRow) > row) || ((pcol + (unsigned int)deltaCol) > col)){
|
||||
deltaRow = 0;
|
||||
deltaCol = 0;
|
||||
}
|
||||
if(maze[((int)prow + deltaRow)][((int)pcol + deltaCol)] == '#'){
|
||||
prow = prow;
|
||||
pcol = pcol;
|
||||
}
|
||||
else{
|
||||
prow = prow + (unsigned int)deltaRow;
|
||||
pcol = pcol + (unsigned int)deltaCol;
|
||||
}
|
||||
if(maze[prow][pcol] == 'T')
|
||||
{
|
||||
maze[prow][pcol] = ' ';
|
||||
score++;
|
||||
}
|
||||
if(maze[prow][pcol] == 'Z')
|
||||
{
|
||||
break;
|
||||
}
|
||||
|
||||
/* LDRA_EXCLUDE 170 S <justification start> FALSE POSITIVE -- needed for ncurses <justification end> */
|
||||
/* LDRA_EXCLUDE 170 S <justification start> FALSE POSITIVE -- needed for ncurses <justification end> */
|
||||
/* LDRA_EXCLUDE 496 S <justification start> FALSE POSITIVE -- needed for ncurses <justification end> */
|
||||
attron(COLOR_PAIR(3));
|
||||
/* LDRA_EXCLUDE 170 S <justification start> FALSE POSITIVE -- needed for ncurses <justification end> */
|
||||
/* LDRA_EXCLUDE 496 S <justification start> FALSE POSITIVE -- needed for ncurses <justification end> */
|
||||
wmove(W,prow,pcol);
|
||||
/* LDRA_EXCLUDE 170 S <justification start> FALSE POSITIVE -- needed for ncurses <justification end> */
|
||||
/* LDRA_EXCLUDE 496 S <justification start> FALSE POSITIVE -- needed for ncurses <justification end> */
|
||||
waddch(W, 'P');
|
||||
/* LDRA_EXCLUDE 170 S <justification start> FALSE POSITIVE -- needed for ncurses <justification end> */
|
||||
/* LDRA_EXCLUDE 170 S <justification start> FALSE POSITIVE -- needed for ncurses <justification end> */
|
||||
/* LDRA_EXCLUDE 496 S <justification start> FALSE POSITIVE -- needed for ncurses <justification end> */
|
||||
/* LDRA_EXCLUDE 496 S <justification start> FALSE POSITIVE -- needed for ncurses <justification end> */
|
||||
attroff(COLOR_PAIR(3));
|
||||
|
||||
deltaRow =0;
|
||||
deltaCol =0;
|
||||
|
||||
/* LDRA_EXCLUDE 170 S <justification start> FALSE POSITIVE -- needed for ncurses <justification end> */
|
||||
/* LDRA_EXCLUDE 496 S <justification start> FALSE POSITIVE -- needed for ncurses <justification end> */
|
||||
wrefresh(W); /* Refresh ncurses window */
|
||||
|
||||
} while (true); /* Repeat until user selects q to quit */
|
||||
|
||||
/* free the memory */
|
||||
for(y = (unsigned)0; y < row; y++)
|
||||
{
|
||||
free(maze[y]);
|
||||
maze[y] = NULL;
|
||||
}
|
||||
if(maze != NULL){
|
||||
free(maze);
|
||||
}
|
||||
/*printw("\n");*/
|
||||
/* LDRA_EXCLUDE 170 S <justification start> FALSE POSITIVE -- needed for ncurses <justification end> */
|
||||
/* LDRA_EXCLUDE 496 S <justification start> FALSE POSITIVE -- needed for ncurses <justification end> */
|
||||
endwin();
|
||||
printf("Score = %u\n", score);
|
||||
return 0;
|
||||
}
|
||||
|
@ -0,0 +1,128 @@
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <stdint.h>
|
||||
#include <string.h>
|
||||
#include <ncurses.h>
|
||||
|
||||
|
||||
|
||||
|
||||
int main(int argc, char *argv[])
|
||||
{
|
||||
FILE* mazeFile; /* Maze Input File */
|
||||
unsigned int row = 0; /* Holds number of rows for input from file */
|
||||
unsigned int col = 0; /* Holds number of cols for input from file */
|
||||
char shold[2000]; /* Holds lines for input from file */
|
||||
unsigned int l = 0;
|
||||
unsigned int k = 0;
|
||||
char **maze; /* array for maze */
|
||||
unsigned int c1 = 0;
|
||||
unsigned int c2 = 0;
|
||||
unsigned int i = 0;
|
||||
unsigned int j = 0;
|
||||
|
||||
/* check to see if argument */
|
||||
if (argc != 2)
|
||||
{
|
||||
printf("An error has ocurred no input file was given.\n");
|
||||
return 1;
|
||||
}
|
||||
mazeFile = fopen(argv[1], "r");
|
||||
if (mazeFile == NULL)
|
||||
{
|
||||
printf("Error in opening the file.\n");
|
||||
return 1;
|
||||
}
|
||||
fgets(shold, 2, mazeFile);
|
||||
while ( feof(mazeFile) == 0 )
|
||||
{
|
||||
|
||||
switch(shold[0])
|
||||
{
|
||||
case 'M':
|
||||
/* read array values, dynamically allocate array */
|
||||
fscanf(mazeFile, "%3u %3u", &row, &col);
|
||||
maze = (char**)malloc(row * 8);
|
||||
for(i = (unsigned) 0; i < row; i++)
|
||||
{
|
||||
if (maze==NULL)
|
||||
{
|
||||
break;
|
||||
}
|
||||
maze[i] = malloc(col * 8);
|
||||
}
|
||||
for (i = (unsigned) 0; i < row; i++)
|
||||
{
|
||||
for (j = (unsigned) 0; j < col; j++)
|
||||
{
|
||||
fscanf(mazeFile, "%c", &maze[i][j]);
|
||||
if (maze[i][j] == '\n')
|
||||
{
|
||||
fscanf(mazeFile, "%c", &maze[i][j]);
|
||||
}
|
||||
}
|
||||
}
|
||||
break;
|
||||
case 'P':
|
||||
fscanf(mazeFile, "%3u %3u", &k, &l);
|
||||
/* check maze bounds */
|
||||
if(k>= row || l>= col){
|
||||
break;
|
||||
}
|
||||
maze[k][l] = 'P';
|
||||
break;
|
||||
|
||||
case 'Z':
|
||||
fscanf(mazeFile, "%3u %3u", &k, &l);
|
||||
/* check maze bounds */
|
||||
if(k>= row || l>= col){
|
||||
break;
|
||||
}
|
||||
maze[k][l] = 'Z';
|
||||
break;
|
||||
|
||||
case 'T':
|
||||
fscanf(mazeFile, "%3u %3u", &k, &l);
|
||||
/* check maze bounds */
|
||||
if(k>= row || l>= col){
|
||||
break;
|
||||
}
|
||||
maze[k][l] = 'T';
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
};
|
||||
fgets(shold, 2, mazeFile);
|
||||
if (feof(mazeFile) != 0)
|
||||
{
|
||||
break;
|
||||
}
|
||||
}
|
||||
fclose(mazeFile);
|
||||
/* print out maze */
|
||||
for (c1 =(unsigned)0; c1 < row; c1++)
|
||||
{
|
||||
printf ("\n");
|
||||
for (c2=(unsigned)0; c2 < col; c2++)
|
||||
{
|
||||
if (maze==NULL)
|
||||
{
|
||||
break;
|
||||
}
|
||||
printf("%c", maze[c1][c2]);
|
||||
}
|
||||
}
|
||||
|
||||
/* free the memory */
|
||||
for(i = (unsigned)0; i < row; i++)
|
||||
{
|
||||
free(maze[i]);
|
||||
maze[i] = NULL;
|
||||
}
|
||||
if(maze != NULL){
|
||||
free(maze);
|
||||
}
|
||||
printf("\n");
|
||||
|
||||
return 0;
|
||||
}
|
@ -0,0 +1,156 @@
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <stdint.h>
|
||||
#include <string.h>
|
||||
|
||||
void readPlayer(char *arr);
|
||||
void readZombie(char *arr);
|
||||
void readTreasure(char *arr);
|
||||
|
||||
// global array for maze
|
||||
char **maze;
|
||||
|
||||
// read maze file in to the array
|
||||
void readMaze(char *arr, int r, int c);
|
||||
|
||||
int main(int argc, char *argv[])
|
||||
{
|
||||
printf("Hello, World!\n");
|
||||
FILE* mazeFile; /* Maze Input File */
|
||||
char c; /* Holds character for line input from file */
|
||||
int row; /* Holds number of rows for input from file */
|
||||
int col; /* Holds number of cols for input from file */
|
||||
char shold[2000]; /* Holds lines for input from file */
|
||||
|
||||
if (argc != 2)
|
||||
{
|
||||
printf("An error has ocurred no input file was given.\n");
|
||||
return 1;
|
||||
}
|
||||
mazeFile = fopen(argv[1], "r");
|
||||
if (mazeFile == NULL)
|
||||
{
|
||||
printf("Error in opening the file.\n");
|
||||
return 1;
|
||||
}
|
||||
fgets(shold, 2, mazeFile);
|
||||
while ( !feof(mazeFile) )
|
||||
{
|
||||
|
||||
switch(shold[0])
|
||||
{
|
||||
case 'M':
|
||||
printf ("Maze encountered\n");
|
||||
fscanf(mazeFile, "%d %d", &row, &col);
|
||||
printf("%d\n", row);
|
||||
// possible dynamic allocation of 2D array
|
||||
// dynamic allocation of array for maze
|
||||
int i = 0, j = 0;
|
||||
maze = malloc(row * sizeof(char *));
|
||||
for(i = 0; i < row; i++)
|
||||
{
|
||||
maze[i] = malloc(col * sizeof(char));
|
||||
}
|
||||
for (i = 0; i < row; i++)
|
||||
{
|
||||
for (j = 0; j < col; j++)
|
||||
{
|
||||
fscanf(mazeFile, "%c", &maze[i][j]);
|
||||
printf("%c", maze[i][j]);
|
||||
}
|
||||
|
||||
}
|
||||
printf("%c", maze[0 * col + 0]);
|
||||
// for(i = 0; i < row; i++)
|
||||
{
|
||||
// free(maze[i]);
|
||||
}
|
||||
// free(maze);
|
||||
// int (*maze)[col] = malloc(sizeof(int[row][col]));
|
||||
//readMaze(maze, row, col);
|
||||
break;
|
||||
case 'P':
|
||||
//case for player
|
||||
printf ("Player\n");
|
||||
fscanf(mazeFile, "%d %d", &row, &col);
|
||||
printf("%d\n", row);
|
||||
// possible dynamic allocation of 2D array
|
||||
// char* player = malloc((row * col) * sizeof(char));
|
||||
// readPlayer(player);
|
||||
break;
|
||||
|
||||
case 'Z':
|
||||
//case for zombie
|
||||
printf ("Zombie\n");
|
||||
fscanf(mazeFile, "%d %d", &row, &col);
|
||||
printf("%d\n", row);
|
||||
// possible dynamic allocation of 2D array
|
||||
// char* zombie = malloc((row * col) * sizeof(char));
|
||||
// readZombie(zombie);
|
||||
break;
|
||||
|
||||
case 'T':
|
||||
//case for treasure
|
||||
printf ("Treasure\n");
|
||||
fscanf(mazeFile, "%d %d", &row, &col);
|
||||
printf("%d\n", row);
|
||||
// possible dynamic allocation of 2D array
|
||||
// char* treasure = malloc((row * col) * sizeof(char));
|
||||
// readTreasure(treasure);
|
||||
break;
|
||||
case '\n':
|
||||
break;
|
||||
default:
|
||||
//printf ("Basic error\n");
|
||||
break;
|
||||
};
|
||||
|
||||
// for(int i=0;i<row;i++)
|
||||
// {
|
||||
// for(int j=0;j<col;j++)
|
||||
// {
|
||||
// if(fscanf == #) //if file has #
|
||||
// {
|
||||
// maze[i][j] = #;
|
||||
// }
|
||||
// else
|
||||
// {
|
||||
// maze[i][j] = " ";
|
||||
// }
|
||||
// }
|
||||
//}
|
||||
|
||||
// fgets(shold, 2, mazeFile);
|
||||
// if (feof(mazeFile))
|
||||
// {
|
||||
// break;
|
||||
//}
|
||||
}
|
||||
fclose(mazeFile);
|
||||
//printf ("So this line won't print without an argument.\n");
|
||||
|
||||
//free the memory
|
||||
// free(maze);
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
void readMaze(char *arr, int row, int col)
|
||||
{
|
||||
|
||||
|
||||
}
|
||||
|
||||
void readPlayer(char *arr)
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
void readZombie(char *arr)
|
||||
{
|
||||
|
||||
}
|
||||
void readTreasure(char *arr)
|
||||
{
|
||||
|
||||
}
|
@ -0,0 +1,142 @@
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <stdint.h>
|
||||
#include <string.h>
|
||||
#include <unistd.h>
|
||||
#include <ncurses.h>
|
||||
|
||||
|
||||
|
||||
|
||||
int main(int argc, char *argv[])
|
||||
{
|
||||
FILE* mazeFile; /* Maze Input File */
|
||||
unsigned int row = 0; /* Holds number of rows for input from file */
|
||||
unsigned int col = 0; /* Holds number of cols for input from file */
|
||||
char shold[2000]; /* Holds lines for input from file */
|
||||
unsigned int l = 0;
|
||||
unsigned int k = 0;
|
||||
char **maze; /* array for maze */
|
||||
unsigned int c1 = 0;
|
||||
unsigned int c2 = 0;
|
||||
unsigned int i = 0;
|
||||
unsigned int j = 0;
|
||||
unsigned int x = 0;
|
||||
unsigned int y = 0;
|
||||
WINDOW* W = 0; /* Curses Window handle */
|
||||
|
||||
/* check to see if argument */
|
||||
if (argc != 2)
|
||||
{
|
||||
printf("An error has ocurred no input file was given.\n");
|
||||
return 1;
|
||||
}
|
||||
mazeFile = fopen(argv[1], "r");
|
||||
if (mazeFile == NULL)
|
||||
{
|
||||
printf("Error in opening the file.\n");
|
||||
return 1;
|
||||
}
|
||||
fgets(shold, 2, mazeFile);
|
||||
while ( feof(mazeFile) == 0 )
|
||||
{
|
||||
|
||||
switch(shold[0])
|
||||
{
|
||||
case 'M':
|
||||
/* read array values, dynamically allocate array */
|
||||
fscanf(mazeFile, "%3u %3u", &row, &col);
|
||||
maze = (char**)malloc(row * 8);
|
||||
for(i = (unsigned) 0; i < row; i++)
|
||||
{
|
||||
if (maze==NULL)
|
||||
{
|
||||
break;
|
||||
}
|
||||
maze[i] = malloc(col * 8);
|
||||
}
|
||||
for (x = (unsigned) 0; x < row; x++)
|
||||
{
|
||||
for (j = (unsigned) 0; j < col; j++)
|
||||
{
|
||||
fscanf(mazeFile, "%c", &maze[x][j]);
|
||||
if (maze[x][j] == '\n')
|
||||
{
|
||||
fscanf(mazeFile, "%c", &maze[x][j]);
|
||||
}
|
||||
}
|
||||
}
|
||||
break;
|
||||
case 'P':
|
||||
fscanf(mazeFile, "%3u %3u", &k, &l);
|
||||
/* check maze bounds */
|
||||
if(k>= row || l>= col){
|
||||
break;
|
||||
}
|
||||
maze[k][l] = 'P';
|
||||
break;
|
||||
|
||||
case 'Z':
|
||||
fscanf(mazeFile, "%3u %3u", &k, &l);
|
||||
/* check maze bounds */
|
||||
if(k>= row || l>= col){
|
||||
break;
|
||||
}
|
||||
maze[k][l] = 'Z';
|
||||
break;
|
||||
|
||||
case 'T':
|
||||
fscanf(mazeFile, "%3u %3u", &k, &l);
|
||||
/* check maze bounds */
|
||||
if(k>= row || l>= col){
|
||||
break;
|
||||
}
|
||||
maze[k][l] = 'T';
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
};
|
||||
fgets(shold, 2, mazeFile);
|
||||
if (feof(mazeFile) != 0)
|
||||
{
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
fclose(mazeFile);
|
||||
/* Initialize ncurses -- you will want to use the following settings */
|
||||
W = initscr(); /* Determine terminal type and initialize curses data structures */
|
||||
start_color(); /* Enable use of color with ncurses */
|
||||
cbreak(); /* Disable line buffering and make char inputs immediately accessible to program */
|
||||
noecho(); /* Disable echo printing of chars type by user */
|
||||
nodelay(W, true); /* Make getch( ) a non-blocking call */
|
||||
keypad(W, true); /* Enable keypad and arrow keys */
|
||||
curs_set(0); /* Set cursor to be invisible - 0 */
|
||||
getmaxyx(W, row, col); /* Query terminal dimensions */
|
||||
/* print out maze */
|
||||
for (c1 =(unsigned)0; c1 < row; c1++)
|
||||
{
|
||||
printf ("\n");
|
||||
for (c2=(unsigned)0; c2 < col; c2++)
|
||||
{
|
||||
if (maze==NULL)
|
||||
{
|
||||
break;
|
||||
}
|
||||
printf("%c", maze[c1][c2]);
|
||||
}
|
||||
}
|
||||
|
||||
/* free the memory */
|
||||
for(y = (unsigned)0; y < row; y++)
|
||||
{
|
||||
free(maze[y]);
|
||||
maze[y] = NULL;
|
||||
}
|
||||
if(maze != NULL){
|
||||
free(maze);
|
||||
}
|
||||
printf("\n");
|
||||
return 0;
|
||||
}
|
@ -0,0 +1,432 @@
|
||||
#include <ncurses.h>
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <stdint.h>
|
||||
#include <string.h>
|
||||
#include <unistd.h>
|
||||
|
||||
extern WINDOW* initscr(void);
|
||||
extern int start_color(void);
|
||||
extern int noecho(void);
|
||||
|
||||
|
||||
/* LDRA_EXCLUDE RULE VIOLATION <justification start> FALSE POSITIVE -- why <justification end> */
|
||||
|
||||
int main(int argc, char *argv[])
|
||||
{
|
||||
FILE* mazeFile; /* Maze Input File */
|
||||
unsigned int row = 0; /* Holds number of rows for input from file */
|
||||
unsigned int col = 0; /* Holds number of cols for input from file */
|
||||
unsigned int pcol = 0;
|
||||
unsigned int prow = 0;
|
||||
char shold[2000]; /* Holds lines for input from file */
|
||||
unsigned int l = 0;
|
||||
unsigned int k = 0;
|
||||
char **maze; /* array for maze */
|
||||
unsigned int c1 = 0;
|
||||
unsigned int c2 = 0;
|
||||
unsigned int i = 0;
|
||||
unsigned int j = 0;
|
||||
unsigned int x = 0;
|
||||
unsigned int y = 0;
|
||||
int deltaRow, deltaCol; /* Change in row-column position based on key input */
|
||||
unsigned int score = 0;
|
||||
|
||||
|
||||
WINDOW* W = 0; /* Curses Window handle */
|
||||
unsigned int rowLimit,colLimit; /* Num of rows and columns in terminal window */
|
||||
/* Initialize ncurses -- you will want to use the following settings */
|
||||
|
||||
|
||||
|
||||
/* check to see if argument */
|
||||
if (argc != 2)
|
||||
{
|
||||
/*printw("An error has ocurred no input file was given.\n");*/
|
||||
return 1;
|
||||
}
|
||||
/* LDRA_EXCLUDE 44 S <justification start> FALSE POSITIVE -- Need function to open the file so must use bad one bc we cannot create one ourselves as we are not wizards <justification end> */
|
||||
mazeFile = fopen(argv[1], "r"); /* open file */
|
||||
if (mazeFile == NULL)
|
||||
{
|
||||
/*printw("Error in opening the file.\n");*/
|
||||
return 1;
|
||||
}
|
||||
/* LDRA_EXCLUDE 44 S <justification start> FALSE POSITIVE -- Need function to get first char from file and we must use bad one as we cannot make one ourselves <justification end> */
|
||||
fgets(shold, 2, mazeFile);
|
||||
while ( feof(mazeFile) == 0 )
|
||||
{
|
||||
|
||||
/* parse maze file,
|
||||
adding chars to array */
|
||||
switch(shold[0])
|
||||
{
|
||||
case 'M':
|
||||
/* read array values, dynamically allocate array */
|
||||
/* LDRA_EXCLUDE 44 S <justification start> FALSE POSITIVE -- Need function to scan content from file, have ensured we will not scan extra <justification end> */
|
||||
fscanf(mazeFile, "%3u %3u", &row, &col);
|
||||
/* LDRA_EXCLUDE 44 S <justification start> FALSE POSITIVE -- Need function to dynamially allocate memory cannot create own as we lack experience <justification end> */
|
||||
maze = (char**)malloc(row * 8); /* allocate memory for rows of array, 8 reperesents size of char* */
|
||||
for(i = (unsigned) 0; i < row; i++)
|
||||
{
|
||||
if (maze==NULL)
|
||||
{
|
||||
break;
|
||||
}
|
||||
/* LDRA_EXCLUDE 44 S <justification start> FALSE POSITIVE -- Need function to dynamially allocate memory cannot create own as we lack experience <justification end> */
|
||||
maze[i] = malloc(col * 8); /* allocate memory for collumns of array, 8 reperesents size of char* */
|
||||
}
|
||||
for (x = (unsigned) 0; x < row; x++)
|
||||
{
|
||||
for (j = (unsigned) 0; j < col; j++)
|
||||
{
|
||||
/* LDRA_EXCLUDE 44 S <justification start> FALSE POSITIVE -- Need function to scan content from file, have ensured we will not scan extra <justification end> */
|
||||
fscanf(mazeFile, "%c", &maze[x][j]);
|
||||
if (maze[x][j] == '\n')
|
||||
{
|
||||
/* LDRA_EXCLUDE 44 S <justification start> FALSE POSITIVE -- Need function to scan content from file, have ensured we will not scan extra <justification end> */
|
||||
fscanf(mazeFile, "%c", &maze[x][j]);
|
||||
}
|
||||
}
|
||||
}
|
||||
break;
|
||||
case 'P':
|
||||
/* LDRA_EXCLUDE 44 S <justification start> FALSE POSITIVE -- Need function to scan content from file, have ensured we will not scan extra <justification end> */
|
||||
fscanf(mazeFile, "%3u %3u", &k, &l);
|
||||
/* check maze bounds */
|
||||
if(k>= row || l>= col){
|
||||
break;
|
||||
}
|
||||
maze[k][l] = 'P';
|
||||
break;
|
||||
|
||||
case 'Z':
|
||||
/* LDRA_EXCLUDE 44 S <justification start> FALSE POSITIVE -- Need function to scan content from file, have ensured we will not scan extra <justification end> */
|
||||
fscanf(mazeFile, "%3u %3u", &k, &l);
|
||||
/* check maze bounds */
|
||||
if(k>= row || l>= col){
|
||||
break;
|
||||
}
|
||||
maze[k][l] = 'Z';
|
||||
break;
|
||||
|
||||
case 'T':
|
||||
/* LDRA_EXCLUDE 44 S <justification start> FALSE POSITIVE -- Need function to scan content from file, have ensured we will not scan extra <justification end> */
|
||||
fscanf(mazeFile, "%3u %3u", &k, &l);
|
||||
/* check maze bounds */
|
||||
if(k>= row || l>= col){
|
||||
break;
|
||||
}
|
||||
maze[k][l] = 'T';
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
};
|
||||
/* LDRA_EXCLUDE 44 S <justification start> FALSE POSITIVE -- Need function to scan content from file, have ensured we will not scan extra <justification end> */
|
||||
fgets(shold, 2, mazeFile);
|
||||
if (feof(mazeFile) != 0)
|
||||
{
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
rowLimit = row;
|
||||
colLimit = col;
|
||||
fclose(mazeFile);
|
||||
/* LDRA_EXCLUDE 170 S <justification start> FALSE POSITIVE -- needed for ncurses <justification end> */
|
||||
/* LDRA_EXCLUDE 496 S <justification start> FALSE POSITIVE -- needed for ncurses <justification end> */
|
||||
/* LDRA_EXCLUDE 41 D <justification start> FALSE POSITIVE -- needed for ncurses <justification end> */
|
||||
W = initscr(); /* Determine terminal type and initialize curses data structures */
|
||||
/* LDRA_EXCLUDE 170 S <justification start> FALSE POSITIVE -- needed for ncurses <justification end> */
|
||||
/* LDRA_EXCLUDE 496 S <justification start> FALSE POSITIVE -- needed for ncurses <justification end> */
|
||||
/* LDRA_EXCLUDE 41 D <justification start> FALSE POSITIVE -- needed for ncurses <justification end> */
|
||||
start_color(); /* Enable use of color with ncurses */
|
||||
/* LDRA_EXCLUDE 170 S <justification start> FALSE POSITIVE -- needed for ncurses <justification end> */
|
||||
/* LDRA_EXCLUDE 496 S <justification start> FALSE POSITIVE -- needed for ncurses <justification end> */
|
||||
/* LDRA_EXCLUDE 41 D <justification start> FALSE POSITIVE -- needed for ncurses <justification end> */
|
||||
cbreak(); /* Disable line buffering and make char inputs immediately accessible to program */
|
||||
/* LDRA_EXCLUDE 170 S <justification start> FALSE POSITIVE -- needed for ncurses <justification end> */
|
||||
/* LDRA_EXCLUDE 496 S <justification start> FALSE POSITIVE -- needed for ncurses <justification end> */
|
||||
/* LDRA_EXCLUDE 41 D <justification start> FALSE POSITIVE -- needed for ncurses <justification end> */
|
||||
noecho(); /* Disable echo printing of chars type by user */
|
||||
if (W == NULL)
|
||||
{
|
||||
return 0;
|
||||
}
|
||||
/* LDRA_EXCLUDE 170 S <justification start> FALSE POSITIVE -- needed for ncurses <justification end> */
|
||||
/* LDRA_EXCLUDE 496 S <justification start> FALSE POSITIVE -- needed for ncurses <justification end> */
|
||||
/* LDRA_EXCLUDE 41 D <justification start> FALSE POSITIVE -- needed for ncurses <justification end> */
|
||||
nodelay(W, true); /* Make getch( ) a non-blocking call */
|
||||
/* LDRA_EXCLUDE 170 S <justification start> FALSE POSITIVE -- needed for ncurses <justification end> */
|
||||
/* LDRA_EXCLUDE 496 S <justification start> FALSE POSITIVE -- needed for ncurses <justification end> */
|
||||
/* LDRA_EXCLUDE 41 D <justification start> FALSE POSITIVE -- needed for ncurses <justification end> */
|
||||
keypad(W, true); /* Enable keypad and arrow keys */
|
||||
/* LDRA_EXCLUDE 170 S <justification start> FALSE POSITIVE -- needed for ncurses <justification end> */
|
||||
/* LDRA_EXCLUDE 496 S <justification start> FALSE POSITIVE -- needed for ncurses <justification end> */
|
||||
/* LDRA_EXCLUDE 41 D <justification start> FALSE POSITIVE -- needed for ncurses <justification end> */
|
||||
curs_set(0); /* Set cursor to be invisible - 0 */
|
||||
/* LDRA_EXCLUDE 170 S <justification start> FALSE POSITIVE -- needed for ncurses <justification end> */
|
||||
/* LDRA_EXCLUDE 496 S <justification start> FALSE POSITIVE -- needed for ncurses <justification end> */
|
||||
/* LDRA_EXCLUDE 41 D <justification start> FALSE POSITIVE -- needed for ncurses <justification end> */
|
||||
getmaxyx(W, rowLimit, colLimit); /* Query terminal dimensions */
|
||||
|
||||
/* Define color pallete foreground-background color pairs */
|
||||
/* PairID#, Foreground Color, Background Color */
|
||||
/* Foreground color == Background color ==> solid color block */
|
||||
/* LDRA_EXCLUDE 170 S <justification start> FALSE POSITIVE -- needed for ncurses <justification end> */
|
||||
/* LDRA_EXCLUDE 496 S <justification start> FALSE POSITIVE -- needed for ncurses <justification end> */
|
||||
/* LDRA_EXCLUDE 41 D <justification start> FALSE POSITIVE -- needed for ncurses <justification end> */
|
||||
init_pair(1, COLOR_BLACK, COLOR_BLACK); /* Black text on Black background */
|
||||
/* LDRA_EXCLUDE 170 S <justification start> FALSE POSITIVE -- needed for ncurses <justification end> */
|
||||
/* LDRA_EXCLUDE 496 S <justification start> FALSE POSITIVE -- needed for ncurses <justification end> */
|
||||
init_pair(2, COLOR_GREEN, COLOR_GREEN); /* Red text on Black background */
|
||||
/* LDRA_EXCLUDE 170 S <justification start> FALSE POSITIVE -- needed for ncurses <justification end> */
|
||||
/* LDRA_EXCLUDE 496 S <justification start> FALSE POSITIVE -- needed for ncurses <justification end> */
|
||||
init_pair(3, COLOR_WHITE, COLOR_WHITE); /* White text on Blue background */
|
||||
/* LDRA_EXCLUDE 170 S <justification start> FALSE POSITIVE -- needed for ncurses <justification end> */
|
||||
/* LDRA_EXCLUDE 496 S <justification start> FALSE POSITIVE -- needed for ncurses <justification end> */
|
||||
init_pair(4, COLOR_RED, COLOR_RED); /* Yellow text on Yellow background */
|
||||
/* LDRA_EXCLUDE 170 S <justification start> FALSE POSITIVE -- needed for ncurses <justification end> */
|
||||
/* LDRA_EXCLUDE 496 S <justification start> FALSE POSITIVE -- needed for ncurses <justification end> */
|
||||
init_pair(5, COLOR_YELLOW, COLOR_YELLOW); /* Yellow text on Yellow background */
|
||||
|
||||
/* start ncurses coloring of maze */
|
||||
for (c1 =(unsigned)0; c1 < row; c1++)
|
||||
{
|
||||
for (c2=(unsigned)0; c2 < col; c2++)
|
||||
{
|
||||
if (maze==NULL)
|
||||
{
|
||||
break;
|
||||
}
|
||||
/* parse maze */
|
||||
switch(maze[c1][c2])
|
||||
{
|
||||
/* color wall green */
|
||||
case '#':
|
||||
/* read array values, dynamically allocate array*/
|
||||
/* LDRA_EXCLUDE 170 S <justification start> FALSE POSITIVE -- needed for ncurses <justification end> */
|
||||
/* LDRA_EXCLUDE 170 S <justification start> FALSE POSITIVE -- needed for ncurses <justification end> */
|
||||
/* LDRA_EXCLUDE 496 S <justification start> FALSE POSITIVE -- needed for ncurses <justification end> */
|
||||
/* LDRA_EXCLUDE 496 S <justification start> FALSE POSITIVE -- needed for ncurses <justification end> */
|
||||
/* LDRA_EXCLUDE 41 D <justification start> FALSE POSITIVE -- needed for ncurses <justification end> */
|
||||
/* LDRA_EXCLUDE 41 D <justification start> FALSE POSITIVE -- needed for ncurses <justification end> */
|
||||
attron(COLOR_PAIR(2));
|
||||
/* LDRA_EXCLUDE 170 S <justification start> FALSE POSITIVE -- needed for ncurses <justification end> */
|
||||
/* LDRA_EXCLUDE 496 S <justification start> FALSE POSITIVE -- needed for ncurses <justification end> */
|
||||
wmove(W, c1,c2);
|
||||
/* LDRA_EXCLUDE 170 S <justification start> FALSE POSITIVE -- needed for ncurses <justification end> */
|
||||
/* LDRA_EXCLUDE 496 S <justification start> FALSE POSITIVE -- needed for ncurses <justification end> */
|
||||
waddch(W,'#');
|
||||
/* LDRA_EXCLUDE 170 S <justification start> FALSE POSITIVE -- needed for ncurses <justification end> */
|
||||
/* LDRA_EXCLUDE 170 S <justification start> FALSE POSITIVE -- needed for ncurses <justification end> */
|
||||
/* LDRA_EXCLUDE 496 S <justification start> FALSE POSITIVE -- needed for ncurses <justification end> */
|
||||
/* LDRA_EXCLUDE 496 S <justification start> FALSE POSITIVE -- needed for ncurses <justification end> */
|
||||
/* LDRA_EXCLUDE 41 D <justification start> FALSE POSITIVE -- needed for ncurses <justification end> */
|
||||
attroff(COLOR_PAIR(2));
|
||||
break;
|
||||
/* place space at position specified in array */
|
||||
case ' ':
|
||||
/* LDRA_EXCLUDE 170 S <justification start> FALSE POSITIVE -- needed for ncurses <justification end> */
|
||||
/* LDRA_EXCLUDE 496 S <justification start> FALSE POSITIVE -- needed for ncurses <justification end> */
|
||||
wmove(W, c1,c2);
|
||||
/* LDRA_EXCLUDE 170 S <justification start> FALSE POSITIVE -- needed for ncurses <justification end> */
|
||||
/* LDRA_EXCLUDE 496 S <justification start> FALSE POSITIVE -- needed for ncurses <justification end> */
|
||||
waddch(W,' ');
|
||||
break;
|
||||
/* place player at position specified in file */
|
||||
case 'P':
|
||||
/* LDRA_EXCLUDE 170 S <justification start> FALSE POSITIVE -- needed for ncurses <justification end> */
|
||||
/* LDRA_EXCLUDE 496 S <justification start> FALSE POSITIVE -- needed for ncurses <justification end> */
|
||||
wmove(W, c1,c2);
|
||||
/* LDRA_EXCLUDE 170 S <justification start> FALSE POSITIVE -- needed for ncurses <justification end> */
|
||||
/* LDRA_EXCLUDE 496 S <justification start> FALSE POSITIVE -- needed for ncurses <justification end> */
|
||||
waddch(W,'P');
|
||||
prow=c1;
|
||||
pcol=c2;
|
||||
break;
|
||||
/* place zombie at position specified in file */
|
||||
case 'Z':
|
||||
/* LDRA_EXCLUDE 170 S <justification start> FALSE POSITIVE -- needed for ncurses <justification end> */
|
||||
/* LDRA_EXCLUDE 170 S <justification start> FALSE POSITIVE -- needed for ncurses <justification end> */
|
||||
/* LDRA_EXCLUDE 496 S <justification start> FALSE POSITIVE -- needed for ncurses <justification end> */
|
||||
/* LDRA_EXCLUDE 496 S <justification start> FALSE POSITIVE -- needed for ncurses <justification end> */
|
||||
attron(COLOR_PAIR(4));
|
||||
/* LDRA_EXCLUDE 170 S <justification start> FALSE POSITIVE -- needed for ncurses <justification end> */
|
||||
/* LDRA_EXCLUDE 496 S <justification start> FALSE POSITIVE -- needed for ncurses <justification end> */
|
||||
wmove(W, c1,c2);
|
||||
/* LDRA_EXCLUDE 170 S <justification start> FALSE POSITIVE -- needed for ncurses <justification end> */
|
||||
/* LDRA_EXCLUDE 496 S <justification start> FALSE POSITIVE -- needed for ncurses <justification end> */
|
||||
waddch(W,'Z');
|
||||
/* LDRA_EXCLUDE 170 S <justification start> FALSE POSITIVE -- needed for ncurses <justification end> */
|
||||
/* LDRA_EXCLUDE 170 S <justification start> FALSE POSITIVE -- needed for ncurses <justification end> */
|
||||
/* LDRA_EXCLUDE 496 S <justification start> FALSE POSITIVE -- needed for ncurses <justification end> */
|
||||
/* LDRA_EXCLUDE 496 S <justification start> FALSE POSITIVE -- needed for ncurses <justification end> */
|
||||
attroff(COLOR_PAIR(4));
|
||||
break;
|
||||
/* place treasure at position specified in file */
|
||||
case 'T':
|
||||
/* LDRA_EXCLUDE 170 S <justification start> FALSE POSITIVE -- needed for ncurses <justification end> */
|
||||
/* LDRA_EXCLUDE 170 S <justification start> FALSE POSITIVE -- needed for ncurses <justification end> */
|
||||
/* LDRA_EXCLUDE 496 S <justification start> FALSE POSITIVE -- needed for ncurses <justification end> */
|
||||
/* LDRA_EXCLUDE 496 S <justification start> FALSE POSITIVE -- needed for ncurses <justification end> */
|
||||
attron(COLOR_PAIR(5));
|
||||
/* LDRA_EXCLUDE 170 S <justification start> FALSE POSITIVE -- needed for ncurses <justification end> */
|
||||
/* LDRA_EXCLUDE 496 S <justification start> FALSE POSITIVE -- needed for ncurses <justification end> */
|
||||
wmove(W, c1,c2);
|
||||
/* LDRA_EXCLUDE 170 S <justification start> FALSE POSITIVE -- needed for ncurses <justification end> */
|
||||
/* LDRA_EXCLUDE 496 S <justification start> FALSE POSITIVE -- needed for ncurses <justification end> */
|
||||
waddch(W,'T');
|
||||
/* LDRA_EXCLUDE 170 S <justification start> FALSE POSITIVE -- needed for ncurses <justification end> */
|
||||
/* LDRA_EXCLUDE 170 S <justification start> FALSE POSITIVE -- needed for ncurses <justification end> */
|
||||
/* LDRA_EXCLUDE 496 S <justification start> FALSE POSITIVE -- needed for ncurses <justification end> */
|
||||
/* LDRA_EXCLUDE 496 S <justification start> FALSE POSITIVE -- needed for ncurses <justification end> */
|
||||
attroff(COLOR_PAIR(5));
|
||||
break;
|
||||
|
||||
default:
|
||||
break;
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
deltaRow = 0;
|
||||
deltaCol = 0;
|
||||
|
||||
|
||||
|
||||
do{
|
||||
|
||||
/* LDRA_EXCLUDE 170 S <justification start> FALSE POSITIVE -- needed for ncurses <justification end> */
|
||||
/* LDRA_EXCLUDE 496 S <justification start> FALSE POSITIVE -- needed for ncurses <justification end> */
|
||||
int kb = wgetch(W); /* Grab keypress */
|
||||
|
||||
/* Support player movement via WASD, IJKL, and arrow keys */
|
||||
if (kb == (int)'a' || kb == (int)'j' || kb == KEY_LEFT)
|
||||
{
|
||||
/* Move Left */
|
||||
deltaRow =0;
|
||||
deltaCol =-1;
|
||||
}
|
||||
else if (kb == (int)'d' || kb == (int)'l' || kb == KEY_RIGHT)
|
||||
{
|
||||
/* Move Right */
|
||||
deltaRow =0;
|
||||
deltaCol =1;
|
||||
}
|
||||
else if (kb == (int)'w' || kb == (int)'i' || kb == KEY_UP)
|
||||
{
|
||||
/* Move Up */
|
||||
deltaRow =-1;
|
||||
deltaCol =0;
|
||||
}
|
||||
else if (kb == (int)'s' || kb == (int)'k' || kb == KEY_DOWN)
|
||||
{
|
||||
/* Move Down */
|
||||
deltaRow =1;
|
||||
deltaCol =0;
|
||||
}
|
||||
else if (kb == (int)'q')
|
||||
{
|
||||
/* q to exit player movement loop */
|
||||
break;
|
||||
};
|
||||
|
||||
/* LDRA_EXCLUDE 170 S <justification start> FALSE POSITIVE -- needed for ncurses <justification end> */
|
||||
/* LDRA_EXCLUDE 170 S <justification start> FALSE POSITIVE -- needed for ncurses <justification end> */
|
||||
/* LDRA_EXCLUDE 496 S <justification start> FALSE POSITIVE -- needed for ncurses <justification end> */
|
||||
/* LDRA_EXCLUDE 496 S <justification start> FALSE POSITIVE -- needed for ncurses <justification end> */
|
||||
attron(COLOR_PAIR(1));
|
||||
/* LDRA_EXCLUDE 170 S <justification start> FALSE POSITIVE -- needed for ncurses <justification end> */
|
||||
/* LDRA_EXCLUDE 496 S <justification start> FALSE POSITIVE -- needed for ncurses <justification end> */
|
||||
wmove(W,prow,pcol);
|
||||
/* LDRA_EXCLUDE 170 S <justification start> FALSE POSITIVE -- needed for ncurses <justification end> */
|
||||
/* LDRA_EXCLUDE 496 S <justification start> FALSE POSITIVE -- needed for ncurses <justification end> */
|
||||
waddch(W, 'P');
|
||||
/* LDRA_EXCLUDE 170 S <justification start> FALSE POSITIVE -- needed for ncurses <justification end> */
|
||||
/* LDRA_EXCLUDE 170 S <justification start> FALSE POSITIVE -- needed for ncurses <justification end> */
|
||||
/* LDRA_EXCLUDE 496 S <justification start> FALSE POSITIVE -- needed for ncurses <justification end> */
|
||||
/* LDRA_EXCLUDE 496 S <justification start> FALSE POSITIVE -- needed for ncurses <justification end> */
|
||||
attroff(COLOR_PAIR(1));
|
||||
/* check current player position against maze bounds,
|
||||
if over the bounds, don't move */
|
||||
if(((prow + (unsigned int)deltaRow) > row) || ((pcol + (unsigned int)deltaCol) > col)){
|
||||
deltaRow = 0;
|
||||
deltaCol = 0;
|
||||
}
|
||||
/* check future player position to see if wall;
|
||||
if it is, stay in the same spot,
|
||||
else move to new postion */
|
||||
if(maze[((int)prow + deltaRow)][((int)pcol + deltaCol)] == '#'){
|
||||
prow = prow;
|
||||
pcol = pcol;
|
||||
}
|
||||
else{
|
||||
prow = prow + (unsigned int)deltaRow;
|
||||
pcol = pcol + (unsigned int)deltaCol;
|
||||
}
|
||||
/* check curremt position for treasure,
|
||||
make treasure position a space,
|
||||
and increment score */
|
||||
if(maze[prow][pcol] == 'T')
|
||||
{
|
||||
maze[prow][pcol] = ' ';
|
||||
score++;
|
||||
}
|
||||
/* check curremt position for zombie,
|
||||
end game */
|
||||
if(maze[prow][pcol] == 'Z')
|
||||
{
|
||||
break;
|
||||
}
|
||||
|
||||
/* LDRA_EXCLUDE 170 S <justification start> FALSE POSITIVE -- needed for ncurses <justification end> */
|
||||
/* LDRA_EXCLUDE 170 S <justification start> FALSE POSITIVE -- needed for ncurses <justification end> */
|
||||
/* LDRA_EXCLUDE 496 S <justification start> FALSE POSITIVE -- needed for ncurses <justification end> */
|
||||
/* LDRA_EXCLUDE 496 S <justification start> FALSE POSITIVE -- needed for ncurses <justification end> */
|
||||
attron(COLOR_PAIR(3));
|
||||
/* LDRA_EXCLUDE 170 S <justification start> FALSE POSITIVE -- needed for ncurses <justification end> */
|
||||
/* LDRA_EXCLUDE 496 S <justification start> FALSE POSITIVE -- needed for ncurses <justification end> */
|
||||
wmove(W,prow,pcol);
|
||||
/* LDRA_EXCLUDE 170 S <justification start> FALSE POSITIVE -- needed for ncurses <justification end> */
|
||||
/* LDRA_EXCLUDE 496 S <justification start> FALSE POSITIVE -- needed for ncurses <justification end> */
|
||||
waddch(W, 'P');
|
||||
/* LDRA_EXCLUDE 170 S <justification start> FALSE POSITIVE -- needed for ncurses <justification end> */
|
||||
/* LDRA_EXCLUDE 170 S <justification start> FALSE POSITIVE -- needed for ncurses <justification end> */
|
||||
/* LDRA_EXCLUDE 496 S <justification start> FALSE POSITIVE -- needed for ncurses <justification end> */
|
||||
/* LDRA_EXCLUDE 496 S <justification start> FALSE POSITIVE -- needed for ncurses <justification end> */
|
||||
attroff(COLOR_PAIR(3));
|
||||
|
||||
deltaRow =0;
|
||||
deltaCol =0;
|
||||
|
||||
/* LDRA_EXCLUDE 170 S <justification start> FALSE POSITIVE -- needed for ncurses <justification end> */
|
||||
/* LDRA_EXCLUDE 496 S <justification start> FALSE POSITIVE -- needed for ncurses <justification end> */
|
||||
wrefresh(W); /* Refresh ncurses window */
|
||||
|
||||
} while (true); /* Repeat until user selects q to quit */
|
||||
|
||||
/* free the memory */
|
||||
for(y = (unsigned)0; y < row; y++)
|
||||
{
|
||||
/* LDRA_EXCLUDE 44 S <justification start> FALSE POSITIVE -- Need function to free allocated memory, cannot create one ourselves bc we are not wizards <justification end> */
|
||||
free(maze[y]);
|
||||
maze[y] = NULL;
|
||||
}
|
||||
if(maze != NULL){
|
||||
/* LDRA_EXCLUDE 44 S <justification start> FALSE POSITIVE -- Need function to free allocated memory, cannot create one ourselves bc we are not wizards <justification end> */
|
||||
free(maze);
|
||||
}
|
||||
/* LDRA_EXCLUDE 170 S <justification start> FALSE POSITIVE -- needed for ncurses <justification end> */
|
||||
/* LDRA_EXCLUDE 496 S <justification start> FALSE POSITIVE -- needed for ncurses <justification end> */
|
||||
/* LDRA_EXCLUDE 41 D <justification start> FALSE POSITIVE -- needed for ncurses <justification end> */
|
||||
endwin();
|
||||
/* LDRA_EXCLUDE 44 S <justification start> FALSE POSITIVE -- Need function to print score for project, so even though not great have to use bad one <justification end> */
|
||||
printf("Score = %u\n", score);
|
||||
return 0;
|
||||
/* LDRA_EXCLUDE 50 D <justification start> FALSE POSITIVE -- Y was statically allocated no need to free it <justification end> */
|
||||
/* LDRA_EXCLUDE 50 D <justification start> FALSE POSITIVE -- Maze has been freed already valgrind shows no leaks error must have ocurred in analysis <justification end> */
|
||||
}
|
||||
|
@ -0,0 +1,18 @@
|
||||
M 12 40
|
||||
########################################
|
||||
# # # #
|
||||
# # # #
|
||||
# # # #
|
||||
# # ###### ############ ### #
|
||||
# # # # # #
|
||||
# # # # # #
|
||||
# # ###### #### ########## #
|
||||
# # # #
|
||||
# # # #
|
||||
# # # #
|
||||
########################################
|
||||
T 8 15
|
||||
Z 8 10
|
||||
P 2 10
|
||||
Z 7 24
|
||||
T 2 20
|
@ -0,0 +1,113 @@
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <stdint.h>
|
||||
#include <string.h>
|
||||
|
||||
|
||||
// global array for maze
|
||||
char **maze;
|
||||
|
||||
|
||||
int main(int argc, char *argv[])
|
||||
{
|
||||
FILE* mazeFile; /* Maze Input File */
|
||||
char chold = NULL; /* Holds character for line input from file */
|
||||
unsigned int row = 0; /* Holds number of rows for input from file */
|
||||
unsigned int col = 0; /* Holds number of cols for input from file */
|
||||
char shold[2000]; /* Holds lines for input from file */
|
||||
unsigned int l = 0;
|
||||
unsigned int k = 0;
|
||||
unsigned int c1 = 0;
|
||||
unsigned int c2 = 0;
|
||||
|
||||
// check to see if
|
||||
if (argc != 2)
|
||||
{
|
||||
printf("An error has ocurred no input file was given.\n");
|
||||
return 1;
|
||||
}
|
||||
mazeFile = fopen(argv[1], "r");
|
||||
if (mazeFile == NULL)
|
||||
{
|
||||
printf("Error in opening the file.\n");
|
||||
return 1;
|
||||
}
|
||||
fgets(shold, 2, mazeFile);
|
||||
while ( feof(mazeFile) == 0 )
|
||||
{
|
||||
|
||||
switch(shold[0])
|
||||
{
|
||||
case 'M':
|
||||
fscanf(mazeFile, "%3d %3d", &row, &col);
|
||||
// dynamic allocation of array for maze
|
||||
int i = 0, j = 0;
|
||||
maze = malloc(row * sizeof(unsigned int *));
|
||||
for(i = 0; i < row; i++)
|
||||
{
|
||||
if (maze==NULL)
|
||||
{
|
||||
break;
|
||||
}
|
||||
maze[i] = malloc(col * sizeof(unsigned int *));
|
||||
}
|
||||
for (i = 0; i < row; i++)
|
||||
{
|
||||
for (j = 0; j < col; j++)
|
||||
{
|
||||
fscanf(mazeFile, "%c", &maze[i][j]);
|
||||
if (maze[i][j] == '\n')
|
||||
{
|
||||
fscanf(mazeFile, "%c", &maze[i][j]);
|
||||
}
|
||||
}
|
||||
}
|
||||
break;
|
||||
case 'P':
|
||||
fscanf(mazeFile, "%3d %3d", &k, &l);
|
||||
maze[k][l] = 'P';
|
||||
break;
|
||||
|
||||
case 'Z':
|
||||
fscanf(mazeFile, "%3d %3d", &k, &l);
|
||||
maze[k][l] = 'Z';
|
||||
break;
|
||||
|
||||
case 'T':
|
||||
fscanf(mazeFile, "%3d %3d", &k, &l);
|
||||
maze[k][l] = 'T';
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
};
|
||||
fgets(shold, 2, mazeFile);
|
||||
if (feof(mazeFile) != 0)
|
||||
{
|
||||
break;
|
||||
}
|
||||
}
|
||||
fclose(mazeFile);
|
||||
for (c1 =0; c1 < row; c1++)
|
||||
{
|
||||
printf ("\n");
|
||||
for (c2=0; c2 < col; c2++)
|
||||
{
|
||||
if (maze==NULL)
|
||||
{
|
||||
break;
|
||||
}
|
||||
printf("%c", maze[c1][c2]);
|
||||
}
|
||||
}
|
||||
|
||||
//free the memory
|
||||
int i = 0;
|
||||
for(i = 0; i < row; i++)
|
||||
{
|
||||
free(maze[i]);
|
||||
}
|
||||
free(maze);
|
||||
printf("\n");
|
||||
|
||||
return 0;
|
||||
}
|
@ -0,0 +1,253 @@
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <stdint.h>
|
||||
#include <string.h>
|
||||
#include <unistd.h>
|
||||
#include <ncurses.h>
|
||||
|
||||
|
||||
/*nothing*/
|
||||
|
||||
int main(int argc, char *argv[])
|
||||
{
|
||||
FILE* mazeFile; /* Maze Input File */
|
||||
unsigned int row = 0; /* Holds number of rows for input from file */
|
||||
unsigned int col = 0; /* Holds number of cols for input from file */
|
||||
unsigned int pcol = 0;
|
||||
unsigned int prow = 0;
|
||||
char shold[2000]; /* Holds lines for input from file */
|
||||
unsigned int l = 0;
|
||||
unsigned int k = 0;
|
||||
char **maze; /* array for maze */
|
||||
unsigned int c1 = 0;
|
||||
unsigned int c2 = 0;
|
||||
unsigned int i = 0;
|
||||
unsigned int j = 0;
|
||||
unsigned int x = 0;
|
||||
unsigned int y = 0;
|
||||
int deltaRow, deltaCol; /* Change in row-column position based on key input */
|
||||
|
||||
|
||||
WINDOW* W = 0; /* Curses Window handle */
|
||||
int rowLimit,colLimit; /* Num of rows and columns in terminal window */
|
||||
/* Initialize ncurses -- you will want to use the following settings */
|
||||
|
||||
|
||||
|
||||
/* check to see if argument */
|
||||
if (argc != 2)
|
||||
{
|
||||
/*printw("An error has ocurred no input file was given.\n");*/
|
||||
return 1;
|
||||
}
|
||||
mazeFile = fopen(argv[1], "r");
|
||||
if (mazeFile == NULL)
|
||||
{
|
||||
/*printw("Error in opening the file.\n");*/
|
||||
return 1;
|
||||
}
|
||||
fgets(shold, 2, mazeFile);
|
||||
while ( feof(mazeFile) == 0 )
|
||||
{
|
||||
|
||||
|
||||
switch(shold[0])
|
||||
{
|
||||
case 'M':
|
||||
/* read array values, dynamically allocate array */
|
||||
fscanf(mazeFile, "%3u %3u", &row, &col);
|
||||
maze = (char**)malloc(row * 8);
|
||||
for(i = (unsigned) 0; i < row; i++)
|
||||
{
|
||||
if (maze==NULL)
|
||||
{
|
||||
break;
|
||||
}
|
||||
maze[i] = malloc(col * 8);
|
||||
}
|
||||
for (x = (unsigned) 0; x < row; x++)
|
||||
{
|
||||
for (j = (unsigned) 0; j < col; j++)
|
||||
{
|
||||
fscanf(mazeFile, "%c", &maze[x][j]);
|
||||
if (maze[x][j] == '\n')
|
||||
{
|
||||
fscanf(mazeFile, "%c", &maze[x][j]);
|
||||
}
|
||||
}
|
||||
}
|
||||
break;
|
||||
case 'P':
|
||||
fscanf(mazeFile, "%3u %3u", &k, &l);
|
||||
/* check maze bounds */
|
||||
if(k>= row || l>= col){
|
||||
break;
|
||||
}
|
||||
maze[k][l] = 'P';
|
||||
break;
|
||||
|
||||
case 'Z':
|
||||
fscanf(mazeFile, "%3u %3u", &k, &l);
|
||||
/* check maze bounds */
|
||||
if(k>= row || l>= col){
|
||||
break;
|
||||
}
|
||||
maze[k][l] = 'Z';
|
||||
break;
|
||||
|
||||
case 'T':
|
||||
fscanf(mazeFile, "%3u %3u", &k, &l);
|
||||
/* check maze bounds */
|
||||
if(k>= row || l>= col){
|
||||
break;
|
||||
}
|
||||
maze[k][l] = 'T';
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
};
|
||||
fgets(shold, 2, mazeFile);
|
||||
if (feof(mazeFile) != 0)
|
||||
{
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
fclose(mazeFile);
|
||||
W = initscr(); /* Determine terminal type and initialize curses data structures */
|
||||
start_color(); /* Enable use of color with ncurses */
|
||||
cbreak(); /* Disable line buffering and make char inputs immediately accessible to program */
|
||||
noecho(); /* Disable echo printing of chars type by user */
|
||||
nodelay(W, true); /* Make getch( ) a non-blocking call */
|
||||
keypad(W, true); /* Enable keypad and arrow keys */
|
||||
curs_set(0); /* Set cursor to be invisible - 0 */
|
||||
getmaxyx(W, rowLimit, colLimit); /* Query terminal dimensions */
|
||||
|
||||
/* Define color pallete foreground-background color pairs */
|
||||
/* PairID#, Foreground Color, Background Color */
|
||||
/* Foreground color == Background color ==> solid color block */
|
||||
init_pair(1, COLOR_BLACK, COLOR_BLACK); /* Black text on Black background */
|
||||
init_pair(2, COLOR_RED, COLOR_BLACK); /* Red text on Black background */
|
||||
init_pair(3, COLOR_WHITE, COLOR_BLUE); /* White text on Blue background */
|
||||
init_pair(4, COLOR_YELLOW, COLOR_YELLOW); /* Yellow text on Yellow background */
|
||||
|
||||
|
||||
for (c1 =(unsigned)0; c1 < row; c1++)
|
||||
{
|
||||
/*printw ("\n");*/
|
||||
for (c2=(unsigned)0; c2 < col; c2++)
|
||||
{
|
||||
if (maze==NULL)
|
||||
{
|
||||
break;
|
||||
}
|
||||
switch(maze[c1][c2])
|
||||
{
|
||||
case '#':
|
||||
/* read array values, dynamically allocate array*/
|
||||
wmove(W, c1,c2);
|
||||
waddch(W,'#');
|
||||
break;
|
||||
case ' ':
|
||||
wmove(W, c1,c2);
|
||||
waddch(W,' ');
|
||||
break;
|
||||
case 'P':
|
||||
wmove(W, c1,c2);
|
||||
waddch(W,'P');
|
||||
prow=c1;
|
||||
pcol=c2;
|
||||
break;
|
||||
case 'Z':
|
||||
wmove(W, c1,c2);
|
||||
waddch(W,'Z');
|
||||
break;
|
||||
case 'T':
|
||||
wmove(W, c1,c2);
|
||||
waddch(W,'T');
|
||||
break;
|
||||
|
||||
default:
|
||||
break;
|
||||
};
|
||||
/*printw("%c", maze[c1][c2]);*/
|
||||
}
|
||||
}
|
||||
|
||||
deltaRow =0;
|
||||
deltaCol =0;
|
||||
|
||||
|
||||
|
||||
do{
|
||||
|
||||
int kb = wgetch(W); /* Grab keypress */
|
||||
|
||||
/* Support player movement via WASD, IJKL, and arrow keys */
|
||||
if (kb == 'a' || kb == 'j' || kb == KEY_LEFT)
|
||||
{
|
||||
/* Move Left */
|
||||
deltaRow =0;
|
||||
deltaCol =-1;
|
||||
}
|
||||
else if (kb == 'd' || kb == 'l' || kb == KEY_RIGHT)
|
||||
{
|
||||
/* Move Right */
|
||||
deltaRow =0;
|
||||
deltaCol =1;
|
||||
}
|
||||
else if (kb == 'w' || kb == 'i' || kb == KEY_UP)
|
||||
{
|
||||
/* Move Up */
|
||||
deltaRow =-1;
|
||||
deltaCol =0;
|
||||
}
|
||||
else if (kb == 's' || kb == 'k' || kb == KEY_DOWN)
|
||||
{
|
||||
/* Move Down */
|
||||
deltaRow =1;
|
||||
deltaCol =0;
|
||||
}
|
||||
else if (kb == 'q')
|
||||
{
|
||||
/* q to exit player movement loop */
|
||||
break;
|
||||
};
|
||||
|
||||
|
||||
attron(COLOR_PAIR(1));
|
||||
wmove(W,prow,pcol);
|
||||
waddch(W, 'P');
|
||||
attroff(COLOR_PAIR(1));
|
||||
|
||||
prow = prow + deltaRow;
|
||||
pcol = pcol + deltaCol;
|
||||
|
||||
attron(COLOR_PAIR(3));
|
||||
wmove(W,prow,pcol);
|
||||
waddch(W, 'P');
|
||||
attroff(COLOR_PAIR(3));
|
||||
|
||||
deltaRow =0;
|
||||
deltaCol =0;
|
||||
|
||||
wrefresh(W); /* Refresh ncurses window */
|
||||
|
||||
} while (true); /* Repeat until user selects q to quit */
|
||||
|
||||
|
||||
/* free the memory */
|
||||
for(y = (unsigned)0; y < row; y++)
|
||||
{
|
||||
free(maze[y]);
|
||||
maze[y] = NULL;
|
||||
}
|
||||
if(maze != NULL){
|
||||
free(maze);
|
||||
}
|
||||
/*printw("\n");*/
|
||||
endwin();
|
||||
return 0;
|
||||
}
|
||||
|
@ -0,0 +1,283 @@
|
||||
#include <ncurses.h>
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <stdint.h>
|
||||
#include <string.h>
|
||||
#include <unistd.h>
|
||||
|
||||
|
||||
|
||||
|
||||
int main(int argc, char *argv[])
|
||||
{
|
||||
FILE* mazeFile; /* Maze Input File */
|
||||
unsigned int row = 0; /* Holds number of rows for input from file */
|
||||
unsigned int col = 0; /* Holds number of cols for input from file */
|
||||
unsigned int pcol = 0;
|
||||
unsigned int prow = 0;
|
||||
char shold[2000]; /* Holds lines for input from file */
|
||||
unsigned int l = 0;
|
||||
unsigned int k = 0;
|
||||
char **maze; /* array for maze */
|
||||
unsigned int c1 = 0;
|
||||
unsigned int c2 = 0;
|
||||
unsigned int i = 0;
|
||||
unsigned int j = 0;
|
||||
unsigned int x = 0;
|
||||
unsigned int y = 0;
|
||||
int deltaRow, deltaCol; /* Change in row-column position based on key input */
|
||||
unsigned int score = 0;
|
||||
|
||||
|
||||
WINDOW* W = 0; /* Curses Window handle */
|
||||
int rowLimit,colLimit; /* Num of rows and columns in terminal window */
|
||||
/* Initialize ncurses -- you will want to use the following settings */
|
||||
|
||||
|
||||
|
||||
/* check to see if argument */
|
||||
if (argc != 2)
|
||||
{
|
||||
/*printw("An error has ocurred no input file was given.\n");*/
|
||||
return 1;
|
||||
}
|
||||
mazeFile = fopen(argv[1], "r");
|
||||
if (mazeFile == NULL)
|
||||
{
|
||||
/*printw("Error in opening the file.\n");*/
|
||||
return 1;
|
||||
}
|
||||
fgets(shold, 2, mazeFile);
|
||||
while ( feof(mazeFile) == 0 )
|
||||
{
|
||||
|
||||
|
||||
switch(shold[0])
|
||||
{
|
||||
case 'M':
|
||||
/* read array values, dynamically allocate array */
|
||||
fscanf(mazeFile, "%3u %3u", &row, &col);
|
||||
maze = (char**)malloc(row * 8);
|
||||
for(i = (unsigned) 0; i < row; i++)
|
||||
{
|
||||
if (maze==NULL)
|
||||
{
|
||||
break;
|
||||
}
|
||||
maze[i] = malloc(col * 8);
|
||||
}
|
||||
for (x = (unsigned) 0; x < row; x++)
|
||||
{
|
||||
for (j = (unsigned) 0; j < col; j++)
|
||||
{
|
||||
fscanf(mazeFile, "%c", &maze[x][j]);
|
||||
if (maze[x][j] == '\n')
|
||||
{
|
||||
fscanf(mazeFile, "%c", &maze[x][j]);
|
||||
}
|
||||
}
|
||||
}
|
||||
break;
|
||||
case 'P':
|
||||
fscanf(mazeFile, "%3u %3u", &k, &l);
|
||||
/* check maze bounds */
|
||||
if(k>= row || l>= col){
|
||||
break;
|
||||
}
|
||||
maze[k][l] = 'P';
|
||||
break;
|
||||
|
||||
case 'Z':
|
||||
fscanf(mazeFile, "%3u %3u", &k, &l);
|
||||
/* check maze bounds */
|
||||
if(k>= row || l>= col){
|
||||
break;
|
||||
}
|
||||
maze[k][l] = 'Z';
|
||||
break;
|
||||
|
||||
case 'T':
|
||||
fscanf(mazeFile, "%3u %3u", &k, &l);
|
||||
/* check maze bounds */
|
||||
if(k>= row || l>= col){
|
||||
break;
|
||||
}
|
||||
maze[k][l] = 'T';
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
};
|
||||
fgets(shold, 2, mazeFile);
|
||||
if (feof(mazeFile) != 0)
|
||||
{
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
fclose(mazeFile);
|
||||
W = initscr(); /* Determine terminal type and initialize curses data structures */
|
||||
start_color(); /* Enable use of color with ncurses */
|
||||
cbreak(); /* Disable line buffering and make char inputs immediately accessible to program */
|
||||
noecho(); /* Disable echo printing of chars type by user */
|
||||
if (W == NULL)
|
||||
{
|
||||
return 0;
|
||||
}
|
||||
nodelay(W, true); /* Make getch( ) a non-blocking call */
|
||||
keypad(W, true); /* Enable keypad and arrow keys */
|
||||
curs_set(0); /* Set cursor to be invisible - 0 */
|
||||
getmaxyx(W, rowLimit, colLimit); /* Query terminal dimensions */
|
||||
|
||||
/* Define color pallete foreground-background color pairs */
|
||||
/* PairID#, Foreground Color, Background Color */
|
||||
/* Foreground color == Background color ==> solid color block */
|
||||
init_pair(1, COLOR_BLACK, COLOR_BLACK); /* Black text on Black background */
|
||||
init_pair(2, COLOR_GREEN, COLOR_GREEN); /* Red text on Black background */
|
||||
init_pair(3, COLOR_WHITE, COLOR_WHITE); /* White text on Blue background */
|
||||
init_pair(4, COLOR_RED, COLOR_RED); /* Yellow text on Yellow background */
|
||||
init_pair(5, COLOR_YELLOW, COLOR_YELLOW); /* Yellow text on Yellow background */
|
||||
|
||||
|
||||
for (c1 =(unsigned)0; c1 < row; c1++)
|
||||
{
|
||||
/*printw ("\n");*/
|
||||
for (c2=(unsigned)0; c2 < col; c2++)
|
||||
{
|
||||
if (maze==NULL)
|
||||
{
|
||||
break;
|
||||
}
|
||||
switch(maze[c1][c2])
|
||||
{
|
||||
case '#':
|
||||
/* read array values, dynamically allocate array*/
|
||||
attron(COLOR_PAIR(2));
|
||||
wmove(W, c1,c2);
|
||||
waddch(W,'#');
|
||||
attroff(COLOR_PAIR(2));
|
||||
break;
|
||||
case ' ':
|
||||
wmove(W, c1,c2);
|
||||
waddch(W,' ');
|
||||
break;
|
||||
case 'P':
|
||||
wmove(W, c1,c2);
|
||||
waddch(W,'P');
|
||||
prow=c1;
|
||||
pcol=c2;
|
||||
break;
|
||||
case 'Z':
|
||||
attron(COLOR_PAIR(4));
|
||||
wmove(W, c1,c2);
|
||||
waddch(W,'Z');
|
||||
attroff(COLOR_PAIR(4));
|
||||
break;
|
||||
case 'T':
|
||||
attron(COLOR_PAIR(5));
|
||||
wmove(W, c1,c2);
|
||||
waddch(W,'T');
|
||||
attroff(COLOR_PAIR(5));
|
||||
break;
|
||||
|
||||
default:
|
||||
break;
|
||||
};
|
||||
/*printw("%c", maze[c1][c2]);*/
|
||||
}
|
||||
}
|
||||
|
||||
deltaRow =0;
|
||||
deltaCol =0;
|
||||
|
||||
|
||||
|
||||
do{
|
||||
|
||||
int kb = wgetch(W); /* Grab keypress */
|
||||
|
||||
/* Support player movement via WASD, IJKL, and arrow keys */
|
||||
if (kb == (int)'a' || kb == (int)'j' || kb == KEY_LEFT)
|
||||
{
|
||||
/* Move Left */
|
||||
deltaRow =0;
|
||||
deltaCol =-1;
|
||||
}
|
||||
else if (kb == (int)'d' || kb == (int)'l' || kb == KEY_RIGHT)
|
||||
{
|
||||
/* Move Right */
|
||||
deltaRow =0;
|
||||
deltaCol =1;
|
||||
}
|
||||
else if (kb == (int)'w' || kb == (int)'i' || kb == KEY_UP)
|
||||
{
|
||||
/* Move Up */
|
||||
deltaRow =-1;
|
||||
deltaCol =0;
|
||||
}
|
||||
else if (kb == (int)'s' || kb == (int)'k' || kb == KEY_DOWN)
|
||||
{
|
||||
/* Move Down */
|
||||
deltaRow =1;
|
||||
deltaCol =0;
|
||||
}
|
||||
else if (kb == (int)'q')
|
||||
{
|
||||
/* q to exit player movement loop */
|
||||
break;
|
||||
};
|
||||
|
||||
attron(COLOR_PAIR(1));
|
||||
wmove(W,prow,pcol);
|
||||
waddch(W, 'P');
|
||||
attroff(COLOR_PAIR(1));
|
||||
|
||||
if(((prow + (unsigned int)deltaRow) > row) || ((pcol + (unsigned int)deltaCol) > col)){
|
||||
deltaRow = 0;
|
||||
deltaCol = 0;
|
||||
}
|
||||
if(maze[((int)prow + deltaRow)][((int)pcol + deltaCol)] == '#'){
|
||||
prow = prow;
|
||||
pcol = pcol;
|
||||
}
|
||||
else{
|
||||
prow = prow + (unsigned int)deltaRow;
|
||||
pcol = pcol + (unsigned int)deltaCol;
|
||||
}
|
||||
if(maze[prow][pcol] == 'T')
|
||||
{
|
||||
maze[prow][pcol] = ' ';
|
||||
score++;
|
||||
}
|
||||
if(maze[prow][pcol] == 'Z')
|
||||
{
|
||||
break;
|
||||
}
|
||||
|
||||
attron(COLOR_PAIR(3));
|
||||
wmove(W,prow,pcol);
|
||||
waddch(W, 'P');
|
||||
attroff(COLOR_PAIR(3));
|
||||
|
||||
deltaRow =0;
|
||||
deltaCol =0;
|
||||
|
||||
wrefresh(W); /* Refresh ncurses window */
|
||||
|
||||
} while (true); /* Repeat until user selects q to quit */
|
||||
|
||||
/* free the memory */
|
||||
for(y = (unsigned)0; y < row; y++)
|
||||
{
|
||||
free(maze[y]);
|
||||
maze[y] = NULL;
|
||||
}
|
||||
if(maze != NULL){
|
||||
free(maze);
|
||||
}
|
||||
/*printw("\n");*/
|
||||
endwin();
|
||||
printf("Score = %u\n", score);
|
||||
return 0;
|
||||
}
|
||||
|
@ -0,0 +1,164 @@
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <stdint.h>
|
||||
#include <string.h>
|
||||
|
||||
void readPlayer(char *arr);
|
||||
void readZombie(char *arr);
|
||||
void readTreasure(char *arr);
|
||||
|
||||
// global array for maze
|
||||
char **maze;
|
||||
|
||||
// read maze file in to the array
|
||||
void readMaze(char *arr, int r, int c);
|
||||
|
||||
int main(int argc, char *argv[])
|
||||
{
|
||||
FILE* mazeFile; /* Maze Input File */
|
||||
char chold; /* Holds character for line input from file */
|
||||
int row; /* Holds number of rows for input from file */
|
||||
int col; /* Holds number of cols for input from file */
|
||||
char shold[2000]; /* Holds lines for input from file */
|
||||
|
||||
if (argc != 2)
|
||||
{
|
||||
printf("An error has ocurred no input file was given.\n");
|
||||
return 1;
|
||||
}
|
||||
mazeFile = fopen(argv[1], "r");
|
||||
if (mazeFile == NULL)
|
||||
{
|
||||
printf("Error in opening the file.\n");
|
||||
return 1;
|
||||
}
|
||||
fgets(shold, 2, mazeFile);
|
||||
while ( !feof(mazeFile) )
|
||||
{
|
||||
|
||||
switch(shold[0])
|
||||
{
|
||||
case 'M':
|
||||
//printf ("Maze encountered\n");
|
||||
fscanf(mazeFile, "%d %d", &row, &col);
|
||||
// printf("%d\n", row);
|
||||
// possible dynamic allocation of 2D array
|
||||
// dynamic allocation of array for maze
|
||||
int i = 0, j = 0;
|
||||
maze = malloc(row * sizeof(char *));
|
||||
for(i = 0; i < row; i++)
|
||||
{
|
||||
maze[i] = malloc(col * sizeof(char));
|
||||
}
|
||||
for (i = 0; i < row; i++)
|
||||
{
|
||||
for (j = 0; j < col; j++)
|
||||
{
|
||||
//fscanf(mazeFile, "%c", &chold);
|
||||
fscanf(mazeFile, "%c", &maze[i][j]);
|
||||
if (maze[i][j] == '\n')
|
||||
{
|
||||
printf ("\n");
|
||||
fscanf(mazeFile, "%c", &maze[i][j]);
|
||||
}
|
||||
printf("%c", maze[i][j]);
|
||||
}
|
||||
|
||||
}
|
||||
// printf("%c", maze[0 * col + 0]);
|
||||
// for(i = 0; i < row; i++)
|
||||
//{
|
||||
// free(maze[i]);
|
||||
//}
|
||||
// free(maze);
|
||||
// int (*maze)[col] = malloc(sizeof(int[row][col]));
|
||||
//readMaze(maze, row, col);
|
||||
break;
|
||||
case 'P':
|
||||
//case for player
|
||||
//printf ("Player\n");
|
||||
fscanf(mazeFile, "%d %d", &row, &col);
|
||||
maze[row][col] = 'P';
|
||||
//printf("%c" , maze[row][col]);
|
||||
//printf("%d\n", row);
|
||||
// possible dynamic allocation of 2D array
|
||||
// char* player = malloc((row * col) * sizeof(char));
|
||||
// readPlayer(player);
|
||||
break;
|
||||
|
||||
case 'Z':
|
||||
//case for zombie
|
||||
printf ("Zombie\n");
|
||||
fscanf(mazeFile, "%d %d", &row, &col);
|
||||
printf("%d\n", row);
|
||||
// possible dynamic allocation of 2D array
|
||||
// char* zombie = malloc((row * col) * sizeof(char));
|
||||
// readZombie(zombie);
|
||||
break;
|
||||
|
||||
case 'T':
|
||||
//case for treasure
|
||||
printf ("Treasure\n");
|
||||
fscanf(mazeFile, "%d %d", &row, &col);
|
||||
printf("%d\n", row);
|
||||
// possible dynamic allocation of 2D array
|
||||
// char* treasure = malloc((row * col) * sizeof(char));
|
||||
// readTreasure(treasure);
|
||||
break;
|
||||
case '\n':
|
||||
printf("\n");
|
||||
break;
|
||||
default:
|
||||
//printf ("Basic error\n");
|
||||
break;
|
||||
};
|
||||
|
||||
// for(int i=0;i<row;i++)
|
||||
// {
|
||||
// for(int j=0;j<col;j++)
|
||||
// {
|
||||
// if(fscanf == #) //if file has #
|
||||
// {
|
||||
// maze[i][j] = #;
|
||||
// }
|
||||
// else
|
||||
// {
|
||||
// maze[i][j] = " ";
|
||||
// }
|
||||
// }
|
||||
//}
|
||||
|
||||
fgets(shold, 2, mazeFile);
|
||||
if (feof(mazeFile))
|
||||
{
|
||||
break;
|
||||
}
|
||||
}
|
||||
fclose(mazeFile);
|
||||
//printf ("So this line won't print without an argument.\n");
|
||||
|
||||
//free the memory
|
||||
// free(maze);
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
void readMaze(char *arr, int row, int col)
|
||||
{
|
||||
|
||||
|
||||
}
|
||||
|
||||
void readPlayer(char *arr)
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
void readZombie(char *arr)
|
||||
{
|
||||
|
||||
}
|
||||
void readTreasure(char *arr)
|
||||
{
|
||||
|
||||
}
|
@ -0,0 +1,113 @@
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <stdint.h>
|
||||
#include <string.h>
|
||||
|
||||
|
||||
// global array for maze
|
||||
char **maze;
|
||||
|
||||
|
||||
int main(int argc, char *argv[])
|
||||
{
|
||||
FILE* mazeFile; /* Maze Input File */
|
||||
char chold = NULL; /* Holds character for line input from file */
|
||||
unsigned int row = 0; /* Holds number of rows for input from file */
|
||||
unsigned int col = 0; /* Holds number of cols for input from file */
|
||||
char shold[2000]; /* Holds lines for input from file */
|
||||
unsigned int l = 0;
|
||||
unsigned int k = 0;
|
||||
unsigned int c1 = 0;
|
||||
unsigned int c2 = 0;
|
||||
|
||||
// check to see if
|
||||
if (argc != 2)
|
||||
{
|
||||
printf("An error has ocurred no input file was given.\n");
|
||||
return 1;
|
||||
}
|
||||
mazeFile = fopen(argv[1], "r");
|
||||
if (mazeFile == NULL)
|
||||
{
|
||||
printf("Error in opening the file.\n");
|
||||
return 1;
|
||||
}
|
||||
fgets(shold, 2, mazeFile);
|
||||
while ( feof(mazeFile) == 0 )
|
||||
{
|
||||
|
||||
switch(shold[0])
|
||||
{
|
||||
case 'M':
|
||||
fscanf(mazeFile, "%3d %3d", &row, &col);
|
||||
// dynamic allocation of array for maze
|
||||
unsigned int i = 0, j = 0;
|
||||
maze = malloc(row * sizeof(unsigned int *));
|
||||
for(i = 0; i < row; i++)
|
||||
{
|
||||
if (maze==NULL)
|
||||
{
|
||||
break;
|
||||
}
|
||||
maze[i] = malloc(col * sizeof(unsigned int *));
|
||||
}
|
||||
for (i = 0; i < row; i++)
|
||||
{
|
||||
for (j = 0; j < col; j++)
|
||||
{
|
||||
fscanf(mazeFile, "%c", &maze[i][j]);
|
||||
if (maze[i][j] == '\n')
|
||||
{
|
||||
fscanf(mazeFile, "%c", &maze[i][j]);
|
||||
}
|
||||
}
|
||||
}
|
||||
break;
|
||||
case 'P':
|
||||
fscanf(mazeFile, "%3d %3d", &k, &l);
|
||||
maze[k][l] = 'P';
|
||||
break;
|
||||
|
||||
case 'Z':
|
||||
fscanf(mazeFile, "%3d %3d", &k, &l);
|
||||
maze[k][l] = 'Z';
|
||||
break;
|
||||
|
||||
case 'T':
|
||||
fscanf(mazeFile, "%3d %3d", &k, &l);
|
||||
maze[k][l] = 'T';
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
};
|
||||
fgets(shold, 2, mazeFile);
|
||||
if (feof(mazeFile) != 0)
|
||||
{
|
||||
break;
|
||||
}
|
||||
}
|
||||
fclose(mazeFile);
|
||||
for (c1 =0; c1 < row; c1++)
|
||||
{
|
||||
printf ("\n");
|
||||
for (c2=0; c2 < col; c2++)
|
||||
{
|
||||
if (maze==NULL)
|
||||
{
|
||||
break;
|
||||
}
|
||||
printf("%c", maze[c1][c2]);
|
||||
}
|
||||
}
|
||||
|
||||
//free the memory
|
||||
unsigned int i = 0;
|
||||
for(i = 0; i < row; i++)
|
||||
{
|
||||
free(maze[i]);
|
||||
}
|
||||
free(maze);
|
||||
printf("\n");
|
||||
|
||||
return 0;
|
||||
}
|
@ -0,0 +1,417 @@
|
||||
#include <ncurses.h>
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <stdint.h>
|
||||
#include <string.h>
|
||||
#include <unistd.h>
|
||||
|
||||
extern WINDOW* initscr(void);
|
||||
extern int start_color(void);
|
||||
extern int noecho(void);
|
||||
|
||||
|
||||
/* LDRA_EXCLUDE RULE VIOLATION <justification start> FALSE POSITIVE -- why <justification end> */
|
||||
|
||||
int main(int argc, char *argv[])
|
||||
{
|
||||
FILE* mazeFile; /* Maze Input File */
|
||||
unsigned int row = 0; /* Holds number of rows for input from file */
|
||||
unsigned int col = 0; /* Holds number of cols for input from file */
|
||||
unsigned int pcol = 0;
|
||||
unsigned int prow = 0;
|
||||
char shold[2000]; /* Holds lines for input from file */
|
||||
unsigned int l = 0;
|
||||
unsigned int k = 0;
|
||||
char **maze; /* array for maze */
|
||||
unsigned int c1 = 0;
|
||||
unsigned int c2 = 0;
|
||||
unsigned int i = 0;
|
||||
unsigned int j = 0;
|
||||
unsigned int x = 0;
|
||||
unsigned int y = 0;
|
||||
int deltaRow, deltaCol; /* Change in row-column position based on key input */
|
||||
unsigned int score = 0;
|
||||
|
||||
|
||||
WINDOW* W = 0; /* Curses Window handle */
|
||||
unsigned int rowLimit,colLimit; /* Num of rows and columns in terminal window */
|
||||
/* Initialize ncurses -- you will want to use the following settings */
|
||||
|
||||
|
||||
|
||||
/* check to see if argument */
|
||||
if (argc != 2)
|
||||
{
|
||||
/*printw("An error has ocurred no input file was given.\n");*/
|
||||
return 1;
|
||||
}
|
||||
/* LDRA_EXCLUDE 44 S <justification start> FALSE POSITIVE -- Need function to open the file so must use bad one bc we cannot create one ourselves as we are not wizards <justification end> */
|
||||
mazeFile = fopen(argv[1], "r");
|
||||
if (mazeFile == NULL)
|
||||
{
|
||||
/*printw("Error in opening the file.\n");*/
|
||||
return 1;
|
||||
}
|
||||
/* LDRA_EXCLUDE 44 S <justification start> FALSE POSITIVE -- Need function to get first char from file and we must use bad one as we cannot make one ourselves <justification end> */
|
||||
fgets(shold, 2, mazeFile);
|
||||
while ( feof(mazeFile) == 0 )
|
||||
{
|
||||
|
||||
|
||||
switch(shold[0])
|
||||
{
|
||||
case 'M':
|
||||
/* read array values, dynamically allocate array */
|
||||
/* LDRA_EXCLUDE 44 S <justification start> FALSE POSITIVE -- Need function to scan content from file, have ensured we will not scan extra <justification end> */
|
||||
fscanf(mazeFile, "%3u %3u", &row, &col);
|
||||
/* LDRA_EXCLUDE 44 S <justification start> FALSE POSITIVE -- Have to use function to dynamically allocate memory even though all are named bad as no other ones exist <justification end> */
|
||||
maze = (char**)malloc(row * 8); /* */
|
||||
for(i = (unsigned) 0; i < row; i++)
|
||||
{
|
||||
if (maze==NULL)
|
||||
{
|
||||
break;
|
||||
}
|
||||
/* LDRA_EXCLUDE 44 S <justification start> FALSE POSITIVE -- Have to use function to dynamically allocate memory even though all are named bad as no other ones exist <justification end> */
|
||||
maze[i] = malloc(col * 8);
|
||||
}
|
||||
for (x = (unsigned) 0; x < row; x++)
|
||||
{
|
||||
for (j = (unsigned) 0; j < col; j++)
|
||||
{
|
||||
/* LDRA_EXCLUDE 44 S <justification start> FALSE POSITIVE -- Need function to scan content from file, have ensured we will not scan extra <justification end> */
|
||||
fscanf(mazeFile, "%c", &maze[x][j]);
|
||||
if (maze[x][j] == '\n')
|
||||
{
|
||||
/* LDRA_EXCLUDE 44 S <justification start> FALSE POSITIVE -- Need function to scan content from file, have ensured we will not scan extra <justification end> */
|
||||
fscanf(mazeFile, "%c", &maze[x][j]);
|
||||
}
|
||||
}
|
||||
}
|
||||
break;
|
||||
case 'P':
|
||||
/* LDRA_EXCLUDE 44 S <justification start> FALSE POSITIVE -- Need function to scan content from file, have ensured we will not scan extra <justification end> */
|
||||
fscanf(mazeFile, "%3u %3u", &k, &l);
|
||||
/* check maze bounds */
|
||||
if(k>= row || l>= col){
|
||||
break;
|
||||
}
|
||||
maze[k][l] = 'P';
|
||||
break;
|
||||
|
||||
case 'Z':
|
||||
/* LDRA_EXCLUDE 44 S <justification start> FALSE POSITIVE -- Need function to scan content from file, have ensured we will not scan extra <justification end> */
|
||||
fscanf(mazeFile, "%3u %3u", &k, &l);
|
||||
/* check maze bounds */
|
||||
if(k>= row || l>= col){
|
||||
break;
|
||||
}
|
||||
maze[k][l] = 'Z';
|
||||
break;
|
||||
|
||||
case 'T':
|
||||
/* LDRA_EXCLUDE 44 S <justification start> FALSE POSITIVE -- Need function to scan content from file, have ensured we will not scan extra <justification end> */
|
||||
fscanf(mazeFile, "%3u %3u", &k, &l);
|
||||
/* check maze bounds */
|
||||
if(k>= row || l>= col){
|
||||
break;
|
||||
}
|
||||
maze[k][l] = 'T';
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
};
|
||||
/* LDRA_EXCLUDE 44 S <justification start> FALSE POSITIVE -- Need function to scan content from file, have ensured we will not scan extra <justification end> */
|
||||
fgets(shold, 2, mazeFile);
|
||||
if (feof(mazeFile) != 0)
|
||||
{
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
rowLimit = row;
|
||||
colLimit = col;
|
||||
fclose(mazeFile);
|
||||
/* LDRA_EXCLUDE 170 S <justification start> FALSE POSITIVE -- needed for ncurses <justification end> */
|
||||
/* LDRA_EXCLUDE 496 S <justification start> FALSE POSITIVE -- needed for ncurses <justification end> */
|
||||
/* LDRA_EXCLUDE 41 D <justification start> FALSE POSITIVE -- needed for ncurses <justification end> */
|
||||
W = initscr(); /* Determine terminal type and initialize curses data structures */
|
||||
/* LDRA_EXCLUDE 170 S <justification start> FALSE POSITIVE -- needed for ncurses <justification end> */
|
||||
/* LDRA_EXCLUDE 496 S <justification start> FALSE POSITIVE -- needed for ncurses <justification end> */
|
||||
/* LDRA_EXCLUDE 41 D <justification start> FALSE POSITIVE -- needed for ncurses <justification end> */
|
||||
start_color(); /* Enable use of color with ncurses */
|
||||
/* LDRA_EXCLUDE 170 S <justification start> FALSE POSITIVE -- needed for ncurses <justification end> */
|
||||
/* LDRA_EXCLUDE 496 S <justification start> FALSE POSITIVE -- needed for ncurses <justification end> */
|
||||
/* LDRA_EXCLUDE 41 D <justification start> FALSE POSITIVE -- needed for ncurses <justification end> */
|
||||
cbreak(); /* Disable line buffering and make char inputs immediately accessible to program */
|
||||
/* LDRA_EXCLUDE 170 S <justification start> FALSE POSITIVE -- needed for ncurses <justification end> */
|
||||
/* LDRA_EXCLUDE 496 S <justification start> FALSE POSITIVE -- needed for ncurses <justification end> */
|
||||
/* LDRA_EXCLUDE 41 D <justification start> FALSE POSITIVE -- needed for ncurses <justification end> */
|
||||
noecho(); /* Disable echo printing of chars type by user */
|
||||
if (W == NULL)
|
||||
{
|
||||
return 0;
|
||||
}
|
||||
/* LDRA_EXCLUDE 170 S <justification start> FALSE POSITIVE -- needed for ncurses <justification end> */
|
||||
/* LDRA_EXCLUDE 496 S <justification start> FALSE POSITIVE -- needed for ncurses <justification end> */
|
||||
/* LDRA_EXCLUDE 41 D <justification start> FALSE POSITIVE -- needed for ncurses <justification end> */
|
||||
nodelay(W, true); /* Make getch( ) a non-blocking call */
|
||||
/* LDRA_EXCLUDE 170 S <justification start> FALSE POSITIVE -- needed for ncurses <justification end> */
|
||||
/* LDRA_EXCLUDE 496 S <justification start> FALSE POSITIVE -- needed for ncurses <justification end> */
|
||||
/* LDRA_EXCLUDE 41 D <justification start> FALSE POSITIVE -- needed for ncurses <justification end> */
|
||||
keypad(W, true); /* Enable keypad and arrow keys */
|
||||
/* LDRA_EXCLUDE 170 S <justification start> FALSE POSITIVE -- needed for ncurses <justification end> */
|
||||
/* LDRA_EXCLUDE 496 S <justification start> FALSE POSITIVE -- needed for ncurses <justification end> */
|
||||
/* LDRA_EXCLUDE 41 D <justification start> FALSE POSITIVE -- needed for ncurses <justification end> */
|
||||
curs_set(0); /* Set cursor to be invisible - 0 */
|
||||
/* LDRA_EXCLUDE 170 S <justification start> FALSE POSITIVE -- needed for ncurses <justification end> */
|
||||
/* LDRA_EXCLUDE 496 S <justification start> FALSE POSITIVE -- needed for ncurses <justification end> */
|
||||
/* LDRA_EXCLUDE 41 D <justification start> FALSE POSITIVE -- needed for ncurses <justification end> */
|
||||
getmaxyx(W, rowLimit, colLimit); /* Query terminal dimensions */
|
||||
|
||||
/* Define color pallete foreground-background color pairs */
|
||||
/* PairID#, Foreground Color, Background Color */
|
||||
/* Foreground color == Background color ==> solid color block */
|
||||
/* LDRA_EXCLUDE 170 S <justification start> FALSE POSITIVE -- needed for ncurses <justification end> */
|
||||
/* LDRA_EXCLUDE 496 S <justification start> FALSE POSITIVE -- needed for ncurses <justification end> */
|
||||
/* LDRA_EXCLUDE 41 D <justification start> FALSE POSITIVE -- needed for ncurses <justification end> */
|
||||
init_pair(1, COLOR_BLACK, COLOR_BLACK); /* Black text on Black background */
|
||||
/* LDRA_EXCLUDE 170 S <justification start> FALSE POSITIVE -- needed for ncurses <justification end> */
|
||||
/* LDRA_EXCLUDE 496 S <justification start> FALSE POSITIVE -- needed for ncurses <justification end> */
|
||||
init_pair(2, COLOR_GREEN, COLOR_GREEN); /* Red text on Black background */
|
||||
/* LDRA_EXCLUDE 170 S <justification start> FALSE POSITIVE -- needed for ncurses <justification end> */
|
||||
/* LDRA_EXCLUDE 496 S <justification start> FALSE POSITIVE -- needed for ncurses <justification end> */
|
||||
init_pair(3, COLOR_WHITE, COLOR_WHITE); /* White text on Blue background */
|
||||
/* LDRA_EXCLUDE 170 S <justification start> FALSE POSITIVE -- needed for ncurses <justification end> */
|
||||
/* LDRA_EXCLUDE 496 S <justification start> FALSE POSITIVE -- needed for ncurses <justification end> */
|
||||
init_pair(4, COLOR_RED, COLOR_RED); /* Yellow text on Yellow background */
|
||||
/* LDRA_EXCLUDE 170 S <justification start> FALSE POSITIVE -- needed for ncurses <justification end> */
|
||||
/* LDRA_EXCLUDE 496 S <justification start> FALSE POSITIVE -- needed for ncurses <justification end> */
|
||||
init_pair(5, COLOR_YELLOW, COLOR_YELLOW); /* Yellow text on Yellow background */
|
||||
|
||||
|
||||
for (c1 =(unsigned)0; c1 < row; c1++)
|
||||
{
|
||||
/*printw ("\n");*/
|
||||
for (c2=(unsigned)0; c2 < col; c2++)
|
||||
{
|
||||
if (maze==NULL)
|
||||
{
|
||||
break;
|
||||
}
|
||||
switch(maze[c1][c2])
|
||||
{
|
||||
case '#':
|
||||
/* read array values, dynamically allocate array*/
|
||||
/* LDRA_EXCLUDE 170 S <justification start> FALSE POSITIVE -- needed for ncurses <justification end> */
|
||||
/* LDRA_EXCLUDE 170 S <justification start> FALSE POSITIVE -- needed for ncurses <justification end> */
|
||||
/* LDRA_EXCLUDE 496 S <justification start> FALSE POSITIVE -- needed for ncurses <justification end> */
|
||||
/* LDRA_EXCLUDE 496 S <justification start> FALSE POSITIVE -- needed for ncurses <justification end> */
|
||||
/* LDRA_EXCLUDE 41 D <justification start> FALSE POSITIVE -- needed for ncurses <justification end> */
|
||||
/* LDRA_EXCLUDE 41 D <justification start> FALSE POSITIVE -- needed for ncurses <justification end> */
|
||||
attron(COLOR_PAIR(2));
|
||||
/* LDRA_EXCLUDE 170 S <justification start> FALSE POSITIVE -- needed for ncurses <justification end> */
|
||||
/* LDRA_EXCLUDE 496 S <justification start> FALSE POSITIVE -- needed for ncurses <justification end> */
|
||||
wmove(W, c1,c2);
|
||||
/* LDRA_EXCLUDE 170 S <justification start> FALSE POSITIVE -- needed for ncurses <justification end> */
|
||||
/* LDRA_EXCLUDE 496 S <justification start> FALSE POSITIVE -- needed for ncurses <justification end> */
|
||||
waddch(W,'#');
|
||||
/* LDRA_EXCLUDE 170 S <justification start> FALSE POSITIVE -- needed for ncurses <justification end> */
|
||||
/* LDRA_EXCLUDE 170 S <justification start> FALSE POSITIVE -- needed for ncurses <justification end> */
|
||||
/* LDRA_EXCLUDE 496 S <justification start> FALSE POSITIVE -- needed for ncurses <justification end> */
|
||||
/* LDRA_EXCLUDE 496 S <justification start> FALSE POSITIVE -- needed for ncurses <justification end> */
|
||||
/* LDRA_EXCLUDE 41 D <justification start> FALSE POSITIVE -- needed for ncurses <justification end> */
|
||||
attroff(COLOR_PAIR(2));
|
||||
break;
|
||||
case ' ':
|
||||
/* LDRA_EXCLUDE 170 S <justification start> FALSE POSITIVE -- needed for ncurses <justification end> */
|
||||
/* LDRA_EXCLUDE 496 S <justification start> FALSE POSITIVE -- needed for ncurses <justification end> */
|
||||
wmove(W, c1,c2);
|
||||
/* LDRA_EXCLUDE 170 S <justification start> FALSE POSITIVE -- needed for ncurses <justification end> */
|
||||
/* LDRA_EXCLUDE 496 S <justification start> FALSE POSITIVE -- needed for ncurses <justification end> */
|
||||
waddch(W,' ');
|
||||
break;
|
||||
case 'P':
|
||||
/* LDRA_EXCLUDE 170 S <justification start> FALSE POSITIVE -- needed for ncurses <justification end> */
|
||||
/* LDRA_EXCLUDE 496 S <justification start> FALSE POSITIVE -- needed for ncurses <justification end> */
|
||||
wmove(W, c1,c2);
|
||||
/* LDRA_EXCLUDE 170 S <justification start> FALSE POSITIVE -- needed for ncurses <justification end> */
|
||||
/* LDRA_EXCLUDE 496 S <justification start> FALSE POSITIVE -- needed for ncurses <justification end> */
|
||||
waddch(W,'P');
|
||||
prow=c1;
|
||||
pcol=c2;
|
||||
break;
|
||||
case 'Z':
|
||||
/* LDRA_EXCLUDE 170 S <justification start> FALSE POSITIVE -- needed for ncurses <justification end> */
|
||||
/* LDRA_EXCLUDE 170 S <justification start> FALSE POSITIVE -- needed for ncurses <justification end> */
|
||||
/* LDRA_EXCLUDE 496 S <justification start> FALSE POSITIVE -- needed for ncurses <justification end> */
|
||||
/* LDRA_EXCLUDE 496 S <justification start> FALSE POSITIVE -- needed for ncurses <justification end> */
|
||||
attron(COLOR_PAIR(4));
|
||||
/* LDRA_EXCLUDE 170 S <justification start> FALSE POSITIVE -- needed for ncurses <justification end> */
|
||||
/* LDRA_EXCLUDE 496 S <justification start> FALSE POSITIVE -- needed for ncurses <justification end> */
|
||||
wmove(W, c1,c2);
|
||||
/* LDRA_EXCLUDE 170 S <justification start> FALSE POSITIVE -- needed for ncurses <justification end> */
|
||||
/* LDRA_EXCLUDE 496 S <justification start> FALSE POSITIVE -- needed for ncurses <justification end> */
|
||||
waddch(W,'Z');
|
||||
/* LDRA_EXCLUDE 170 S <justification start> FALSE POSITIVE -- needed for ncurses <justification end> */
|
||||
/* LDRA_EXCLUDE 170 S <justification start> FALSE POSITIVE -- needed for ncurses <justification end> */
|
||||
/* LDRA_EXCLUDE 496 S <justification start> FALSE POSITIVE -- needed for ncurses <justification end> */
|
||||
/* LDRA_EXCLUDE 496 S <justification start> FALSE POSITIVE -- needed for ncurses <justification end> */
|
||||
attroff(COLOR_PAIR(4));
|
||||
break;
|
||||
case 'T':
|
||||
/* LDRA_EXCLUDE 170 S <justification start> FALSE POSITIVE -- needed for ncurses <justification end> */
|
||||
/* LDRA_EXCLUDE 170 S <justification start> FALSE POSITIVE -- needed for ncurses <justification end> */
|
||||
/* LDRA_EXCLUDE 496 S <justification start> FALSE POSITIVE -- needed for ncurses <justification end> */
|
||||
/* LDRA_EXCLUDE 496 S <justification start> FALSE POSITIVE -- needed for ncurses <justification end> */
|
||||
attron(COLOR_PAIR(5));
|
||||
/* LDRA_EXCLUDE 170 S <justification start> FALSE POSITIVE -- needed for ncurses <justification end> */
|
||||
/* LDRA_EXCLUDE 496 S <justification start> FALSE POSITIVE -- needed for ncurses <justification end> */
|
||||
wmove(W, c1,c2);
|
||||
/* LDRA_EXCLUDE 170 S <justification start> FALSE POSITIVE -- needed for ncurses <justification end> */
|
||||
/* LDRA_EXCLUDE 496 S <justification start> FALSE POSITIVE -- needed for ncurses <justification end> */
|
||||
waddch(W,'T');
|
||||
/* LDRA_EXCLUDE 170 S <justification start> FALSE POSITIVE -- needed for ncurses <justification end> */
|
||||
/* LDRA_EXCLUDE 170 S <justification start> FALSE POSITIVE -- needed for ncurses <justification end> */
|
||||
/* LDRA_EXCLUDE 496 S <justification start> FALSE POSITIVE -- needed for ncurses <justification end> */
|
||||
/* LDRA_EXCLUDE 496 S <justification start> FALSE POSITIVE -- needed for ncurses <justification end> */
|
||||
attroff(COLOR_PAIR(5));
|
||||
break;
|
||||
|
||||
default:
|
||||
break;
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
deltaRow =0;
|
||||
deltaCol =0;
|
||||
|
||||
|
||||
|
||||
do{
|
||||
|
||||
/* LDRA_EXCLUDE 170 S <justification start> FALSE POSITIVE -- needed for ncurses <justification end> */
|
||||
/* LDRA_EXCLUDE 496 S <justification start> FALSE POSITIVE -- needed for ncurses <justification end> */
|
||||
int kb = wgetch(W); /* Grab keypress */
|
||||
|
||||
/* Support player movement via WASD, IJKL, and arrow keys */
|
||||
if (kb == (int)'a' || kb == (int)'j' || kb == KEY_LEFT)
|
||||
{
|
||||
/* Move Left */
|
||||
deltaRow =0;
|
||||
deltaCol =-1;
|
||||
}
|
||||
else if (kb == (int)'d' || kb == (int)'l' || kb == KEY_RIGHT)
|
||||
{
|
||||
/* Move Right */
|
||||
deltaRow =0;
|
||||
deltaCol =1;
|
||||
}
|
||||
else if (kb == (int)'w' || kb == (int)'i' || kb == KEY_UP)
|
||||
{
|
||||
/* Move Up */
|
||||
deltaRow =-1;
|
||||
deltaCol =0;
|
||||
}
|
||||
else if (kb == (int)'s' || kb == (int)'k' || kb == KEY_DOWN)
|
||||
{
|
||||
/* Move Down */
|
||||
deltaRow =1;
|
||||
deltaCol =0;
|
||||
}
|
||||
else if (kb == (int)'q')
|
||||
{
|
||||
/* q to exit player movement loop */
|
||||
break;
|
||||
};
|
||||
|
||||
/* LDRA_EXCLUDE 170 S <justification start> FALSE POSITIVE -- needed for ncurses <justification end> */
|
||||
/* LDRA_EXCLUDE 170 S <justification start> FALSE POSITIVE -- needed for ncurses <justification end> */
|
||||
/* LDRA_EXCLUDE 496 S <justification start> FALSE POSITIVE -- needed for ncurses <justification end> */
|
||||
/* LDRA_EXCLUDE 496 S <justification start> FALSE POSITIVE -- needed for ncurses <justification end> */
|
||||
attron(COLOR_PAIR(1));
|
||||
/* LDRA_EXCLUDE 170 S <justification start> FALSE POSITIVE -- needed for ncurses <justification end> */
|
||||
/* LDRA_EXCLUDE 496 S <justification start> FALSE POSITIVE -- needed for ncurses <justification end> */
|
||||
wmove(W,prow,pcol);
|
||||
/* LDRA_EXCLUDE 170 S <justification start> FALSE POSITIVE -- needed for ncurses <justification end> */
|
||||
/* LDRA_EXCLUDE 496 S <justification start> FALSE POSITIVE -- needed for ncurses <justification end> */
|
||||
waddch(W, 'P');
|
||||
/* LDRA_EXCLUDE 170 S <justification start> FALSE POSITIVE -- needed for ncurses <justification end> */
|
||||
/* LDRA_EXCLUDE 170 S <justification start> FALSE POSITIVE -- needed for ncurses <justification end> */
|
||||
/* LDRA_EXCLUDE 496 S <justification start> FALSE POSITIVE -- needed for ncurses <justification end> */
|
||||
/* LDRA_EXCLUDE 496 S <justification start> FALSE POSITIVE -- needed for ncurses <justification end> */
|
||||
attroff(COLOR_PAIR(1));
|
||||
|
||||
if(((prow + (unsigned int)deltaRow) > row) || ((pcol + (unsigned int)deltaCol) > col)){
|
||||
deltaRow = 0;
|
||||
deltaCol = 0;
|
||||
}
|
||||
if(maze[((int)prow + deltaRow)][((int)pcol + deltaCol)] == '#'){
|
||||
prow = prow;
|
||||
pcol = pcol;
|
||||
}
|
||||
else{
|
||||
prow = prow + (unsigned int)deltaRow;
|
||||
pcol = pcol + (unsigned int)deltaCol;
|
||||
}
|
||||
if(maze[prow][pcol] == 'T')
|
||||
{
|
||||
maze[prow][pcol] = ' ';
|
||||
score++;
|
||||
}
|
||||
if(maze[prow][pcol] == 'Z')
|
||||
{
|
||||
break;
|
||||
}
|
||||
|
||||
/* LDRA_EXCLUDE 170 S <justification start> FALSE POSITIVE -- needed for ncurses <justification end> */
|
||||
/* LDRA_EXCLUDE 170 S <justification start> FALSE POSITIVE -- needed for ncurses <justification end> */
|
||||
/* LDRA_EXCLUDE 496 S <justification start> FALSE POSITIVE -- needed for ncurses <justification end> */
|
||||
/* LDRA_EXCLUDE 496 S <justification start> FALSE POSITIVE -- needed for ncurses <justification end> */
|
||||
attron(COLOR_PAIR(3));
|
||||
/* LDRA_EXCLUDE 170 S <justification start> FALSE POSITIVE -- needed for ncurses <justification end> */
|
||||
/* LDRA_EXCLUDE 496 S <justification start> FALSE POSITIVE -- needed for ncurses <justification end> */
|
||||
wmove(W,prow,pcol);
|
||||
/* LDRA_EXCLUDE 170 S <justification start> FALSE POSITIVE -- needed for ncurses <justification end> */
|
||||
/* LDRA_EXCLUDE 496 S <justification start> FALSE POSITIVE -- needed for ncurses <justification end> */
|
||||
waddch(W, 'P');
|
||||
/* LDRA_EXCLUDE 170 S <justification start> FALSE POSITIVE -- needed for ncurses <justification end> */
|
||||
/* LDRA_EXCLUDE 170 S <justification start> FALSE POSITIVE -- needed for ncurses <justification end> */
|
||||
/* LDRA_EXCLUDE 496 S <justification start> FALSE POSITIVE -- needed for ncurses <justification end> */
|
||||
/* LDRA_EXCLUDE 496 S <justification start> FALSE POSITIVE -- needed for ncurses <justification end> */
|
||||
attroff(COLOR_PAIR(3));
|
||||
|
||||
deltaRow =0;
|
||||
deltaCol =0;
|
||||
|
||||
/* LDRA_EXCLUDE 170 S <justification start> FALSE POSITIVE -- needed for ncurses <justification end> */
|
||||
/* LDRA_EXCLUDE 496 S <justification start> FALSE POSITIVE -- needed for ncurses <justification end> */
|
||||
wrefresh(W); /* Refresh ncurses window */
|
||||
|
||||
} while (true); /* Repeat until user selects q to quit */
|
||||
|
||||
/* free the memory */
|
||||
for(y = (unsigned)0; y < row; y++)
|
||||
{
|
||||
/* LDRA_EXCLUDE 44 S <justification start> FALSE POSITIVE -- Need function to free allocated memory, cannot create one ourselves bc we are not wizards <justification end> */
|
||||
free(maze[y]);
|
||||
maze[y] = NULL;
|
||||
}
|
||||
if(maze != NULL){
|
||||
/* LDRA_EXCLUDE 44 S <justification start> FALSE POSITIVE -- Need function to free allocated memory, cannot create one ourselves bc we are not wizards <justification end> */
|
||||
free(maze);
|
||||
}
|
||||
/*printw("\n");*/
|
||||
/* LDRA_EXCLUDE 170 S <justification start> FALSE POSITIVE -- needed for ncurses <justification end> */
|
||||
/* LDRA_EXCLUDE 496 S <justification start> FALSE POSITIVE -- needed for ncurses <justification end> */
|
||||
/* LDRA_EXCLUDE 41 D <justification start> FALSE POSITIVE -- needed for ncurses <justification end> */
|
||||
endwin();
|
||||
/* LDRA_EXCLUDE 44 S <justification start> FALSE POSITIVE -- Need function to print score for project, so even though not great have to use bad one <justification end> */
|
||||
printf("Score = %u\n", score);
|
||||
return 0;
|
||||
/* LDRA_EXCLUDE 50 D <justification start> FALSE POSITIVE -- Y was statically allocated no need to free it <justification end> */
|
||||
/* LDRA_EXCLUDE 50 D <justification start> FALSE POSITIVE -- Maze has been freed already valgrind shows no leaks error must have ocurred in analysis <justification end> */
|
||||
}
|
||||
|
@ -0,0 +1,118 @@
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <stdint.h>
|
||||
#include <string.h>
|
||||
|
||||
|
||||
// global array for maze
|
||||
char **maze;
|
||||
|
||||
|
||||
int main(int argc, char *argv[])
|
||||
{
|
||||
FILE* mazeFile; /* Maze Input File */
|
||||
char chold; /* Holds character for line input from file */
|
||||
int row; /* Holds number of rows for input from file */
|
||||
int col; /* Holds number of cols for input from file */
|
||||
char shold[2000]; /* Holds lines for input from file */
|
||||
int l;
|
||||
int k;
|
||||
int c1;
|
||||
int c2;
|
||||
|
||||
if (argc != 2)
|
||||
{
|
||||
printf("An error has ocurred no input file was given.\n");
|
||||
return 1;
|
||||
}
|
||||
mazeFile = fopen(argv[1], "r");
|
||||
if (mazeFile == NULL)
|
||||
{
|
||||
printf("Error in opening the file.\n");
|
||||
return 1;
|
||||
}
|
||||
fgets(shold, 2, mazeFile);
|
||||
while ( !feof(mazeFile) )
|
||||
{
|
||||
|
||||
switch(shold[0])
|
||||
{
|
||||
case 'M':
|
||||
//printf ("Maze encountered\n");
|
||||
fscanf(mazeFile, "%d %d", &row, &col);
|
||||
// printf("%d\n", row);
|
||||
// possible dynamic allocation of 2D array
|
||||
// dynamic allocation of array for maze
|
||||
int i = 0, j = 0;
|
||||
maze = malloc(row * sizeof(char *));
|
||||
for(i = 0; i < row; i++)
|
||||
{
|
||||
maze[i] = malloc(col * sizeof(char));
|
||||
}
|
||||
for (i = 0; i < row; i++)
|
||||
{
|
||||
for (j = 0; j < col; j++)
|
||||
{
|
||||
//fscanf(mazeFile, "%c", &chold);
|
||||
fscanf(mazeFile, "%c", &maze[i][j]);
|
||||
if (maze[i][j] == '\n')
|
||||
{
|
||||
printf ("\n");
|
||||
fscanf(mazeFile, "%c", &maze[i][j]);
|
||||
}
|
||||
printf("%c", maze[i][j]);
|
||||
}
|
||||
}
|
||||
break;
|
||||
case 'P':
|
||||
fscanf(mazeFile, "%d %d", &k, &l);
|
||||
maze[k][l] = 'P';
|
||||
printf("%c" , maze[k][l]);
|
||||
break;
|
||||
|
||||
case 'Z':
|
||||
fscanf(mazeFile, "%d %d", &k, &l);
|
||||
maze[k][l] = 'Z';
|
||||
printf("%c" , maze[k][l]);
|
||||
break;
|
||||
|
||||
case 'T':
|
||||
//case for treasure
|
||||
printf ("Treasure\n");
|
||||
fscanf(mazeFile, "%d %d", &k, &l);
|
||||
maze[k][l] = 'T';
|
||||
printf("%c" , maze[k][l]);
|
||||
break;
|
||||
case '\n':
|
||||
printf("\n");
|
||||
break;
|
||||
default:
|
||||
//printf ("Basic error\n");
|
||||
break;
|
||||
};
|
||||
fgets(shold, 2, mazeFile);
|
||||
if (feof(mazeFile))
|
||||
{
|
||||
break;
|
||||
}
|
||||
}
|
||||
fclose(mazeFile);
|
||||
for (c1 =0; c1 < row; c1++)
|
||||
{
|
||||
printf ("\n");
|
||||
for (c2=0; c2 < col; c2++)
|
||||
{
|
||||
printf("%c", maze[c1][c2]);
|
||||
}
|
||||
}
|
||||
//printf ("So this line won't print without an argument.\n");
|
||||
|
||||
//free the memory
|
||||
int i = 0;
|
||||
for(i = 0; i < row; i++)
|
||||
free(maze[i]);
|
||||
free(maze);
|
||||
printf("\n");
|
||||
|
||||
return 0;
|
||||
}
|
@ -0,0 +1,113 @@
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <stdint.h>
|
||||
#include <string.h>
|
||||
|
||||
|
||||
// global array for maze
|
||||
char **maze;
|
||||
|
||||
|
||||
int main(int argc, char *argv[])
|
||||
{
|
||||
FILE* mazeFile; /* Maze Input File */
|
||||
char chold = NULL; /* Holds character for line input from file */
|
||||
int row = 0; /* Holds number of rows for input from file */
|
||||
int col = 0; /* Holds number of cols for input from file */
|
||||
char shold[2000]; /* Holds lines for input from file */
|
||||
int l = 0;
|
||||
int k = 0;
|
||||
int c1 = 0;
|
||||
int c2 = 0;
|
||||
|
||||
// check to see if
|
||||
if (argc != 2)
|
||||
{
|
||||
printf("An error has ocurred no input file was given.\n");
|
||||
return 1;
|
||||
}
|
||||
mazeFile = fopen(argv[1], "r");
|
||||
if (mazeFile == NULL)
|
||||
{
|
||||
printf("Error in opening the file.\n");
|
||||
return 1;
|
||||
}
|
||||
fgets(shold, 2, mazeFile);
|
||||
while ( !feof(mazeFile) )
|
||||
{
|
||||
|
||||
switch(shold[0])
|
||||
{
|
||||
case 'M':
|
||||
fscanf(mazeFile, "%3d %3d", &row, &col);
|
||||
// dynamic allocation of array for maze
|
||||
int i = 0, j = 0;
|
||||
maze = malloc(row * sizeof(unsigned int *));
|
||||
for(i = 0; i < row; i++)
|
||||
{
|
||||
if (maze==NULL)
|
||||
{
|
||||
break;
|
||||
}
|
||||
maze[i] = malloc(col * sizeof(unsigned int *));
|
||||
}
|
||||
for (i = 0; i < row; i++)
|
||||
{
|
||||
for (j = 0; j < col; j++)
|
||||
{
|
||||
fscanf(mazeFile, "%c", &maze[i][j]);
|
||||
if (maze[i][j] == '\n')
|
||||
{
|
||||
fscanf(mazeFile, "%c", &maze[i][j]);
|
||||
}
|
||||
}
|
||||
}
|
||||
break;
|
||||
case 'P':
|
||||
fscanf(mazeFile, "%3d %3d", &k, &l);
|
||||
maze[k][l] = 'P';
|
||||
break;
|
||||
|
||||
case 'Z':
|
||||
fscanf(mazeFile, "%3d %3d", &k, &l);
|
||||
maze[k][l] = 'Z';
|
||||
break;
|
||||
|
||||
case 'T':
|
||||
fscanf(mazeFile, "%3d %3d", &k, &l);
|
||||
maze[k][l] = 'T';
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
};
|
||||
fgets(shold, 2, mazeFile);
|
||||
if (feof(mazeFile))
|
||||
{
|
||||
break;
|
||||
}
|
||||
}
|
||||
fclose(mazeFile);
|
||||
for (c1 =0; c1 < row; c1++)
|
||||
{
|
||||
printf ("\n");
|
||||
for (c2=0; c2 < col; c2++)
|
||||
{
|
||||
if (maze==NULL)
|
||||
{
|
||||
break;
|
||||
}
|
||||
printf("%c", maze[c1][c2]);
|
||||
}
|
||||
}
|
||||
|
||||
//free the memory
|
||||
int i = 0;
|
||||
for(i = 0; i < row; i++)
|
||||
{
|
||||
free(maze[i]);
|
||||
}
|
||||
free(maze);
|
||||
printf("\n");
|
||||
|
||||
return 0;
|
||||
}
|
@ -0,0 +1,254 @@
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <stdint.h>
|
||||
#include <string.h>
|
||||
#include <unistd.h>
|
||||
#include <ncurses.h>
|
||||
|
||||
|
||||
|
||||
|
||||
int main(int argc, char *argv[])
|
||||
{
|
||||
FILE* mazeFile; /* Maze Input File */
|
||||
unsigned int row = 0; /* Holds number of rows for input from file */
|
||||
unsigned int col = 0; /* Holds number of cols for input from file */
|
||||
unsigned int pcol = 0;
|
||||
unsigned int prow = 0;
|
||||
char shold[2000]; /* Holds lines for input from file */
|
||||
unsigned int l = 0;
|
||||
unsigned int k = 0;
|
||||
char **maze; /* array for maze */
|
||||
unsigned int c1 = 0;
|
||||
unsigned int c2 = 0;
|
||||
unsigned int i = 0;
|
||||
unsigned int j = 0;
|
||||
unsigned int x = 0;
|
||||
unsigned int y = 0;
|
||||
int deltaRow, deltaCol; /* Change in row-column position based on key input */
|
||||
|
||||
|
||||
WINDOW* W = 0; /* Curses Window handle */
|
||||
int rowLimit,colLimit; /* Num of rows and columns in terminal window */
|
||||
/* Initialize ncurses -- you will want to use the following settings */
|
||||
|
||||
|
||||
|
||||
/* check to see if argument */
|
||||
if (argc != 2)
|
||||
{
|
||||
/*printw("An error has ocurred no input file was given.\n");*/
|
||||
return 1;
|
||||
}
|
||||
mazeFile = fopen(argv[1], "r");
|
||||
if (mazeFile == NULL)
|
||||
{
|
||||
/*printw("Error in opening the file.\n");*/
|
||||
return 1;
|
||||
}
|
||||
fgets(shold, 2, mazeFile);
|
||||
while ( feof(mazeFile) == 0 )
|
||||
{
|
||||
|
||||
|
||||
switch(shold[0])
|
||||
{
|
||||
case 'M':
|
||||
/* read array values, dynamically allocate array */
|
||||
fscanf(mazeFile, "%3u %3u", &row, &col);
|
||||
maze = (char**)malloc(row * 8);
|
||||
for(i = (unsigned) 0; i < row; i++)
|
||||
{
|
||||
if (maze==NULL)
|
||||
{
|
||||
break;
|
||||
}
|
||||
maze[i] = malloc(col * 8);
|
||||
}
|
||||
for (x = (unsigned) 0; x < row; x++)
|
||||
{
|
||||
for (j = (unsigned) 0; j < col; j++)
|
||||
{
|
||||
fscanf(mazeFile, "%c", &maze[x][j]);
|
||||
if (maze[x][j] == '\n')
|
||||
{
|
||||
fscanf(mazeFile, "%c", &maze[x][j]);
|
||||
}
|
||||
}
|
||||
}
|
||||
break;
|
||||
case 'P':
|
||||
fscanf(mazeFile, "%3u %3u", &k, &l);
|
||||
/* check maze bounds */
|
||||
if(k>= row || l>= col){
|
||||
break;
|
||||
}
|
||||
maze[k][l] = 'P';
|
||||
break;
|
||||
|
||||
case 'Z':
|
||||
fscanf(mazeFile, "%3u %3u", &k, &l);
|
||||
/* check maze bounds */
|
||||
if(k>= row || l>= col){
|
||||
break;
|
||||
}
|
||||
maze[k][l] = 'Z';
|
||||
break;
|
||||
|
||||
case 'T':
|
||||
fscanf(mazeFile, "%3u %3u", &k, &l);
|
||||
/* check maze bounds */
|
||||
if(k>= row || l>= col){
|
||||
break;
|
||||
}
|
||||
maze[k][l] = 'T';
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
};
|
||||
fgets(shold, 2, mazeFile);
|
||||
if (feof(mazeFile) != 0)
|
||||
{
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
fclose(mazeFile);
|
||||
W = initscr(); /* Determine terminal type and initialize curses data structures */
|
||||
start_color(); /* Enable use of color with ncurses */
|
||||
cbreak(); /* Disable line buffering and make char inputs immediately accessible to program */
|
||||
noecho(); /* Disable echo printing of chars type by user */
|
||||
nodelay(W, true); /* Make getch( ) a non-blocking call */
|
||||
keypad(W, true); /* Enable keypad and arrow keys */
|
||||
curs_set(0); /* Set cursor to be invisible - 0 */
|
||||
getmaxyx(W, rowLimit, colLimit); /* Query terminal dimensions */
|
||||
|
||||
/* Define color pallete foreground-background color pairs */
|
||||
/* PairID#, Foreground Color, Background Color */
|
||||
/* Foreground color == Background color ==> solid color block */
|
||||
init_pair(1, COLOR_BLACK, COLOR_BLACK); /* Black text on Black background */
|
||||
init_pair(2, COLOR_RED, COLOR_BLACK); /* Red text on Black background */
|
||||
init_pair(3, COLOR_WHITE, COLOR_BLUE); /* White text on Blue background */
|
||||
init_pair(4, COLOR_YELLOW, COLOR_YELLOW); /* Yellow text on Yellow background */
|
||||
|
||||
|
||||
for (c1 =(unsigned)0; c1 < row; c1++)
|
||||
{
|
||||
/*printw ("\n");*/
|
||||
for (c2=(unsigned)0; c2 < col; c2++)
|
||||
{
|
||||
if (maze==NULL)
|
||||
{
|
||||
break;
|
||||
}
|
||||
switch(maze[c1][c2])
|
||||
{
|
||||
case '#':
|
||||
/* read array values, dynamically allocate array*/
|
||||
wmove(W, c1,c2);
|
||||
waddch(W,'#');
|
||||
break;
|
||||
case ' ':
|
||||
wmove(W, c1,c2);
|
||||
waddch(W,' ');
|
||||
break;
|
||||
case 'P':
|
||||
wmove(W, c1,c2);
|
||||
waddch(W,'P');
|
||||
prow=c1;
|
||||
pcol=c2;
|
||||
break;
|
||||
case 'Z':
|
||||
wmove(W, c1,c2);
|
||||
waddch(W,'Z');
|
||||
break;
|
||||
case 'T':
|
||||
wmove(W, c1,c2);
|
||||
waddch(W,'T');
|
||||
break;
|
||||
|
||||
default:
|
||||
break;
|
||||
};
|
||||
/*printw("%c", maze[c1][c2]);*/
|
||||
}
|
||||
}
|
||||
|
||||
prow = rowLimit/4;
|
||||
pcol = colLimit/4;
|
||||
deltaRow =0;
|
||||
deltaCol =0;
|
||||
|
||||
|
||||
|
||||
do{
|
||||
|
||||
int kb = wgetch(W); /* Grab keypress */
|
||||
|
||||
/* Support player movement via WASD, IJKL, and arrow keys */
|
||||
if (kb == 'a' || kb == 'j' || kb == KEY_LEFT)
|
||||
{
|
||||
/* Move Left */
|
||||
deltaRow =0;
|
||||
deltaCol =-1;
|
||||
}
|
||||
else if (kb == 'd' || kb == 'l' || kb == KEY_RIGHT)
|
||||
{
|
||||
/* Move Right */
|
||||
deltaRow =0;
|
||||
deltaCol =1;
|
||||
}
|
||||
else if (kb == 'w' || kb == 'i' || kb == KEY_UP)
|
||||
{
|
||||
/* Move Up */
|
||||
deltaRow =-1;
|
||||
deltaCol =0;
|
||||
}
|
||||
else if (kb == 's' || kb == 'k' || kb == KEY_DOWN)
|
||||
{
|
||||
/* Move Down */
|
||||
deltaRow =1;
|
||||
deltaCol =0;
|
||||
}
|
||||
else if (kb == 'q')
|
||||
{
|
||||
/* q to exit player movement loop */
|
||||
break;
|
||||
};
|
||||
|
||||
|
||||
attron(COLOR_PAIR(1));
|
||||
wmove(W,prow,pcol);
|
||||
waddch(W, 'P');
|
||||
attroff(COLOR_PAIR(1));
|
||||
|
||||
prow = prow + deltaRow;
|
||||
pcol = pcol + deltaCol;
|
||||
|
||||
attron(COLOR_PAIR(1));
|
||||
wmove(W,prow,pcol);
|
||||
waddch(W, 'P');
|
||||
attroff(COLOR_PAIR(1));
|
||||
|
||||
deltaRow =0;
|
||||
deltaCol =0;
|
||||
|
||||
wrefresh(W); /* Refresh ncurses window */
|
||||
|
||||
} while (true); /* Repeat until user selects q to quit */
|
||||
|
||||
|
||||
/* free the memory */
|
||||
for(y = (unsigned)0; y < row; y++)
|
||||
{
|
||||
free(maze[y]);
|
||||
maze[y] = NULL;
|
||||
}
|
||||
if(maze != NULL){
|
||||
free(maze);
|
||||
}
|
||||
/*printw("\n");*/
|
||||
endwin();
|
||||
return 0;
|
||||
}
|
@ -0,0 +1,15 @@
|
||||
M 12 40
|
||||
########################################
|
||||
# # # #
|
||||
# # # #
|
||||
# # # #
|
||||
# # ###### ############ ### #
|
||||
# # # # # #
|
||||
# # # # # #
|
||||
# # ###### #### ########## #
|
||||
# # # #
|
||||
# # # #
|
||||
# # # #
|
||||
########################################
|
||||
P 2 10
|
||||
Z 8 10
|
@ -0,0 +1,27 @@
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <stdint.h>
|
||||
#include <string.h>
|
||||
|
||||
int main(int argc, char const *argv[])
|
||||
{
|
||||
printf("Hello, World!\n");
|
||||
FILE* mazeFile; /* Maze Input File */
|
||||
char c; /* Holds character for line input from file */
|
||||
int row; /* Holds number of rows for input from file */
|
||||
int col; /* Holds number of cols for input from file */
|
||||
if (argc != 2)
|
||||
{
|
||||
printf("An error has ocurred no input file was given.\n");
|
||||
return 1;
|
||||
}
|
||||
mazeFile = fopen(argv[1], "r");
|
||||
if (mazeFile == NULL)
|
||||
{
|
||||
printf("Error in opening the file.\n");
|
||||
return 1;
|
||||
}
|
||||
fclose(mazeFile);
|
||||
printf ("So this line won't print without an argument.\n");
|
||||
return 0;
|
||||
}
|
@ -0,0 +1,150 @@
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <stdint.h>
|
||||
#include <string.h>
|
||||
|
||||
void readPlayer(char *arr);
|
||||
void readZombie(char *arr);
|
||||
void readTreasure(char *arr);
|
||||
|
||||
// global array for maze
|
||||
char **maze;
|
||||
|
||||
// read maze file in to the array
|
||||
void readMaze(char *arr, int r, int c);
|
||||
|
||||
int main(int argc, char *argv[])
|
||||
{
|
||||
printf("Hello, World!\n");
|
||||
FILE* mazeFile; /* Maze Input File */
|
||||
char c; /* Holds character for line input from file */
|
||||
int row; /* Holds number of rows for input from file */
|
||||
int col; /* Holds number of cols for input from file */
|
||||
char shold[2000]; /* Holds lines for input from file */
|
||||
|
||||
if (argc != 2)
|
||||
{
|
||||
printf("An error has ocurred no input file was given.\n");
|
||||
return 1;
|
||||
}
|
||||
mazeFile = fopen(argv[1], "r");
|
||||
if (mazeFile == NULL)
|
||||
{
|
||||
printf("Error in opening the file.\n");
|
||||
return 1;
|
||||
}
|
||||
fgets(shold, 2, mazeFile);
|
||||
while ( !feof(mazeFile) )
|
||||
{
|
||||
|
||||
switch(shold[0])
|
||||
{
|
||||
case 'M':
|
||||
printf ("Maze encountered\n");
|
||||
fscanf(mazeFile, "%d %d", &row, &col);
|
||||
printf("%d\n", row);
|
||||
// possible dynamic allocation of 2D array
|
||||
// dynamic allocation of array for maze
|
||||
int i = 0, j = 0;
|
||||
maze = malloc(row * sizeof(char *));
|
||||
for(i = 0; i < row; i++)
|
||||
maze[i] = malloc(col * sizeof(char));
|
||||
for (i = 0; i < row; i++)
|
||||
{
|
||||
for (j = 0; j < col; j++)
|
||||
{
|
||||
fscanf(mazeFile, "%c", &maze[i][j]);
|
||||
printf("%c", maze[i][j]);
|
||||
}
|
||||
|
||||
}
|
||||
printf("%c", maze[0 * col + 0]);
|
||||
for(i = 0; i < row; i++)
|
||||
free(maze[i]);
|
||||
free(maze);
|
||||
//int (*maze)[col] = malloc(sizeof(int[row][col]));
|
||||
//readMaze(maze, row, col);
|
||||
break;
|
||||
case 'P':
|
||||
//case for player
|
||||
printf ("Player\n");
|
||||
fscanf(mazeFile, "%d %d", &row, &col);
|
||||
printf("%d\n", row);
|
||||
// possible dynamic allocation of 2D array
|
||||
char* player = malloc((row * col) * sizeof(char));
|
||||
readPlayer(player);
|
||||
break;
|
||||
|
||||
case 'Z':
|
||||
//case for zombie
|
||||
printf ("Zombie\n");
|
||||
fscanf(mazeFile, "%d %d", &row, &col);
|
||||
printf("%d\n", row);
|
||||
// possible dynamic allocation of 2D array
|
||||
char* zombie = malloc((row * col) * sizeof(char));
|
||||
readZombie(zombie);
|
||||
break;
|
||||
|
||||
case 'T':
|
||||
//case for treasure
|
||||
printf ("Treasure\n");
|
||||
fscanf(mazeFile, "%d %d", &row, &col);
|
||||
printf("%d\n", row);
|
||||
// possible dynamic allocation of 2D array
|
||||
char* treasure = malloc((row * col) * sizeof(char));
|
||||
readTreasure(treasure);
|
||||
break;
|
||||
default:
|
||||
//printf ("Basic error\n");
|
||||
break;
|
||||
};
|
||||
|
||||
// for(int i=0;i<row;i++)
|
||||
// {
|
||||
// for(int j=0;j<col;j++)
|
||||
// {
|
||||
// if(fscanf == #) //if file has #
|
||||
// {
|
||||
// maze[i][j] = #;
|
||||
// }
|
||||
// else
|
||||
// {
|
||||
// maze[i][j] = " ";
|
||||
// }
|
||||
// }
|
||||
//}
|
||||
|
||||
// fgets(shold, 2, mazeFile);
|
||||
// if (feof(mazeFile))
|
||||
// {
|
||||
// break;
|
||||
//}
|
||||
}
|
||||
fclose(mazeFile);
|
||||
//printf ("So this line won't print without an argument.\n");
|
||||
|
||||
//free the memory
|
||||
// free(maze);
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
void readMaze(char *arr, int row, int col)
|
||||
{
|
||||
|
||||
|
||||
}
|
||||
|
||||
void readPlayer(char *arr)
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
void readZombie(char *arr)
|
||||
{
|
||||
|
||||
}
|
||||
void readTreasure(char *arr)
|
||||
{
|
||||
|
||||
}
|
@ -0,0 +1,260 @@
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <stdint.h>
|
||||
#include <string.h>
|
||||
#include <unistd.h>
|
||||
#include <ncurses.h>
|
||||
|
||||
|
||||
/*nothing*/
|
||||
|
||||
int main(int argc, char *argv[])
|
||||
{
|
||||
FILE* mazeFile; /* Maze Input File */
|
||||
unsigned int row = 0; /* Holds number of rows for input from file */
|
||||
unsigned int col = 0; /* Holds number of cols for input from file */
|
||||
unsigned int pcol = 0;
|
||||
unsigned int prow = 0;
|
||||
char shold[2000]; /* Holds lines for input from file */
|
||||
unsigned int l = 0;
|
||||
unsigned int k = 0;
|
||||
char **maze; /* array for maze */
|
||||
unsigned int c1 = 0;
|
||||
unsigned int c2 = 0;
|
||||
unsigned int i = 0;
|
||||
unsigned int j = 0;
|
||||
unsigned int x = 0;
|
||||
unsigned int y = 0;
|
||||
int deltaRow, deltaCol; /* Change in row-column position based on key input */
|
||||
|
||||
|
||||
WINDOW* W = 0; /* Curses Window handle */
|
||||
int rowLimit,colLimit; /* Num of rows and columns in terminal window */
|
||||
/* Initialize ncurses -- you will want to use the following settings */
|
||||
|
||||
|
||||
|
||||
/* check to see if argument */
|
||||
if (argc != 2)
|
||||
{
|
||||
/*printw("An error has ocurred no input file was given.\n");*/
|
||||
return 1;
|
||||
}
|
||||
mazeFile = fopen(argv[1], "r");
|
||||
if (mazeFile == NULL)
|
||||
{
|
||||
/*printw("Error in opening the file.\n");*/
|
||||
return 1;
|
||||
}
|
||||
fgets(shold, 2, mazeFile);
|
||||
while ( feof(mazeFile) == 0 )
|
||||
{
|
||||
|
||||
|
||||
switch(shold[0])
|
||||
{
|
||||
case 'M':
|
||||
/* read array values, dynamically allocate array */
|
||||
fscanf(mazeFile, "%3u %3u", &row, &col);
|
||||
maze = (char**)malloc(row * 8);
|
||||
for(i = (unsigned) 0; i < row; i++)
|
||||
{
|
||||
if (maze==NULL)
|
||||
{
|
||||
break;
|
||||
}
|
||||
maze[i] = malloc(col * 8);
|
||||
}
|
||||
for (x = (unsigned) 0; x < row; x++)
|
||||
{
|
||||
for (j = (unsigned) 0; j < col; j++)
|
||||
{
|
||||
fscanf(mazeFile, "%c", &maze[x][j]);
|
||||
if (maze[x][j] == '\n')
|
||||
{
|
||||
fscanf(mazeFile, "%c", &maze[x][j]);
|
||||
}
|
||||
}
|
||||
}
|
||||
break;
|
||||
case 'P':
|
||||
fscanf(mazeFile, "%3u %3u", &k, &l);
|
||||
/* check maze bounds */
|
||||
if(k>= row || l>= col){
|
||||
break;
|
||||
}
|
||||
maze[k][l] = 'P';
|
||||
break;
|
||||
|
||||
case 'Z':
|
||||
fscanf(mazeFile, "%3u %3u", &k, &l);
|
||||
/* check maze bounds */
|
||||
if(k>= row || l>= col){
|
||||
break;
|
||||
}
|
||||
maze[k][l] = 'Z';
|
||||
break;
|
||||
|
||||
case 'T':
|
||||
fscanf(mazeFile, "%3u %3u", &k, &l);
|
||||
/* check maze bounds */
|
||||
if(k>= row || l>= col){
|
||||
break;
|
||||
}
|
||||
maze[k][l] = 'T';
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
};
|
||||
fgets(shold, 2, mazeFile);
|
||||
if (feof(mazeFile) != 0)
|
||||
{
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
fclose(mazeFile);
|
||||
W = initscr(); /* Determine terminal type and initialize curses data structures */
|
||||
start_color(); /* Enable use of color with ncurses */
|
||||
cbreak(); /* Disable line buffering and make char inputs immediately accessible to program */
|
||||
noecho(); /* Disable echo printing of chars type by user */
|
||||
nodelay(W, true); /* Make getch( ) a non-blocking call */
|
||||
keypad(W, true); /* Enable keypad and arrow keys */
|
||||
curs_set(0); /* Set cursor to be invisible - 0 */
|
||||
getmaxyx(W, rowLimit, colLimit); /* Query terminal dimensions */
|
||||
|
||||
/* Define color pallete foreground-background color pairs */
|
||||
/* PairID#, Foreground Color, Background Color */
|
||||
/* Foreground color == Background color ==> solid color block */
|
||||
init_pair(1, COLOR_BLACK, COLOR_BLACK); /* Black text on Black background */
|
||||
init_pair(2, COLOR_GREEN, COLOR_GREEN); /* Red text on Black background */
|
||||
init_pair(3, COLOR_WHITE, COLOR_WHITE); /* White text on Blue background */
|
||||
init_pair(4, COLOR_RED, COLOR_RED); /* Yellow text on Yellow background */
|
||||
init_pair(5, COLOR_YELLOW, COLOR_YELLOW); /* Yellow text on Yellow background */
|
||||
|
||||
|
||||
for (c1 =(unsigned)0; c1 < row; c1++)
|
||||
{
|
||||
/*printw ("\n");*/
|
||||
for (c2=(unsigned)0; c2 < col; c2++)
|
||||
{
|
||||
if (maze==NULL)
|
||||
{
|
||||
break;
|
||||
}
|
||||
switch(maze[c1][c2])
|
||||
{
|
||||
case '#':
|
||||
/* read array values, dynamically allocate array*/
|
||||
attron(COLOR_PAIR(2));
|
||||
wmove(W, c1,c2);
|
||||
waddch(W,'#');
|
||||
attroff(COLOR_PAIR(2));
|
||||
break;
|
||||
case ' ':
|
||||
wmove(W, c1,c2);
|
||||
waddch(W,' ');
|
||||
break;
|
||||
case 'P':
|
||||
wmove(W, c1,c2);
|
||||
waddch(W,'P');
|
||||
prow=c1;
|
||||
pcol=c2;
|
||||
break;
|
||||
case 'Z':
|
||||
attron(COLOR_PAIR(4));
|
||||
wmove(W, c1,c2);
|
||||
waddch(W,'Z');
|
||||
attroff(COLOR_PAIR(4));
|
||||
break;
|
||||
case 'T':
|
||||
attron(COLOR_PAIR(5));
|
||||
wmove(W, c1,c2);
|
||||
waddch(W,'T');
|
||||
attroff(COLOR_PAIR(5));
|
||||
break;
|
||||
|
||||
default:
|
||||
break;
|
||||
};
|
||||
/*printw("%c", maze[c1][c2]);*/
|
||||
}
|
||||
}
|
||||
|
||||
deltaRow =0;
|
||||
deltaCol =0;
|
||||
|
||||
|
||||
|
||||
do{
|
||||
|
||||
int kb = wgetch(W); /* Grab keypress */
|
||||
|
||||
/* Support player movement via WASD, IJKL, and arrow keys */
|
||||
if (kb == 'a' || kb == 'j' || kb == KEY_LEFT)
|
||||
{
|
||||
/* Move Left */
|
||||
deltaRow =0;
|
||||
deltaCol =-1;
|
||||
}
|
||||
else if (kb == 'd' || kb == 'l' || kb == KEY_RIGHT)
|
||||
{
|
||||
/* Move Right */
|
||||
deltaRow =0;
|
||||
deltaCol =1;
|
||||
}
|
||||
else if (kb == 'w' || kb == 'i' || kb == KEY_UP)
|
||||
{
|
||||
/* Move Up */
|
||||
deltaRow =-1;
|
||||
deltaCol =0;
|
||||
}
|
||||
else if (kb == 's' || kb == 'k' || kb == KEY_DOWN)
|
||||
{
|
||||
/* Move Down */
|
||||
deltaRow =1;
|
||||
deltaCol =0;
|
||||
}
|
||||
else if (kb == 'q')
|
||||
{
|
||||
/* q to exit player movement loop */
|
||||
break;
|
||||
};
|
||||
|
||||
|
||||
attron(COLOR_PAIR(1));
|
||||
wmove(W,prow,pcol);
|
||||
waddch(W, 'P');
|
||||
attroff(COLOR_PAIR(1));
|
||||
|
||||
prow = prow + deltaRow;
|
||||
pcol = pcol + deltaCol;
|
||||
|
||||
attron(COLOR_PAIR(3));
|
||||
wmove(W,prow,pcol);
|
||||
waddch(W, 'P');
|
||||
attroff(COLOR_PAIR(3));
|
||||
|
||||
deltaRow =0;
|
||||
deltaCol =0;
|
||||
|
||||
wrefresh(W); /* Refresh ncurses window */
|
||||
|
||||
} while (true); /* Repeat until user selects q to quit */
|
||||
|
||||
|
||||
/* free the memory */
|
||||
for(y = (unsigned)0; y < row; y++)
|
||||
{
|
||||
free(maze[y]);
|
||||
maze[y] = NULL;
|
||||
}
|
||||
if(maze != NULL){
|
||||
free(maze);
|
||||
}
|
||||
/*printw("\n");*/
|
||||
endwin();
|
||||
return 0;
|
||||
}
|
||||
|
@ -0,0 +1,114 @@
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <stdint.h>
|
||||
#include <string.h>
|
||||
|
||||
|
||||
|
||||
|
||||
int main(int argc, char *argv[])
|
||||
{
|
||||
FILE* mazeFile; /* Maze Input File */
|
||||
char chold = NULL; /* Holds character for line input from file */
|
||||
unsigned int row = 0; /* Holds number of rows for input from file */
|
||||
unsigned int col = 0; /* Holds number of cols for input from file */
|
||||
char shold[2000]; /* Holds lines for input from file */
|
||||
unsigned int l = 0;
|
||||
unsigned int k = 0;
|
||||
char **maze; // global array for maze
|
||||
unsigned int c1 = 0;
|
||||
unsigned int c2 = 0;
|
||||
|
||||
// check to see if
|
||||
if (argc != 2)
|
||||
{
|
||||
printf("An error has ocurred no input file was given.\n");
|
||||
return 1;
|
||||
}
|
||||
mazeFile = fopen(argv[1], "r");
|
||||
if (mazeFile == NULL)
|
||||
{
|
||||
printf("Error in opening the file.\n");
|
||||
return 1;
|
||||
}
|
||||
fgets(shold, 2, mazeFile);
|
||||
while ( feof(mazeFile) == 0 )
|
||||
{
|
||||
|
||||
switch(shold[0])
|
||||
{
|
||||
case 'M':
|
||||
fscanf(mazeFile, "%3d %3d", &row, &col);
|
||||
// dynamic allocation of array for maze
|
||||
unsigned int i = 0, j = 0;
|
||||
maze = malloc(row * sizeof(unsigned int *));
|
||||
for(i = (unsigned) 0; i < row; i++)
|
||||
{
|
||||
if (maze==NULL)
|
||||
{
|
||||
break;
|
||||
}
|
||||
maze[i] = malloc(col * sizeof(unsigned int *));
|
||||
}
|
||||
for (i = (unsigned) 0; i < row; i++)
|
||||
{
|
||||
for (j = (unsigned) 0; j < col; j++)
|
||||
{
|
||||
fscanf(mazeFile, "%c", &maze[i][j]);
|
||||
if (maze[i][j] == '\n')
|
||||
{
|
||||
fscanf(mazeFile, "%c", &maze[i][j]);
|
||||
}
|
||||
}
|
||||
}
|
||||
break;
|
||||
case 'P':
|
||||
fscanf(mazeFile, "%3d %3d", &k, &l);
|
||||
maze[k][l] = 'P';
|
||||
break;
|
||||
|
||||
case 'Z':
|
||||
fscanf(mazeFile, "%3d %3d", &k, &l);
|
||||
maze[k][l] = 'Z';
|
||||
break;
|
||||
|
||||
case 'T':
|
||||
fscanf(mazeFile, "%3d %3d", &k, &l);
|
||||
maze[k][l] = 'T';
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
};
|
||||
fgets(shold, 2, mazeFile);
|
||||
if (feof(mazeFile) != 0)
|
||||
{
|
||||
break;
|
||||
}
|
||||
}
|
||||
fclose(mazeFile);
|
||||
for (c1 =(unsigned)0; c1 < row; c1++)
|
||||
{
|
||||
printf ("\n");
|
||||
for (c2=(unsigned)0; c2 < col; c2++)
|
||||
{
|
||||
if (maze==NULL)
|
||||
{
|
||||
break;
|
||||
}
|
||||
printf("%c", maze[c1][c2]);
|
||||
}
|
||||
}
|
||||
|
||||
//free the memory
|
||||
unsigned int i = 0;
|
||||
for(i = (unsigned)0; i < row; i++)
|
||||
{
|
||||
free(maze[i]);
|
||||
}
|
||||
if(maze != NULL){
|
||||
free(maze);
|
||||
}
|
||||
printf("\n");
|
||||
|
||||
return 0;
|
||||
}
|
@ -0,0 +1,3 @@
|
||||
|
||||
game:
|
||||
gcc game.c -o game
|
@ -0,0 +1,397 @@
|
||||
#include <ncurses.h>
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <stdint.h>
|
||||
#include <string.h>
|
||||
#include <unistd.h>
|
||||
|
||||
|
||||
/* LDRA_EXCLUDE RULE VIOLATION <justification start> FALSE POSITIVE -- why <justification end> */
|
||||
|
||||
int main(int argc, char *argv[])
|
||||
{
|
||||
FILE* mazeFile; /* Maze Input File */
|
||||
unsigned int row = 0; /* Holds number of rows for input from file */
|
||||
unsigned int col = 0; /* Holds number of cols for input from file */
|
||||
unsigned int pcol = 0;
|
||||
unsigned int prow = 0;
|
||||
char shold[2000]; /* Holds lines for input from file */
|
||||
unsigned int l = 0;
|
||||
unsigned int k = 0;
|
||||
char **maze; /* array for maze */
|
||||
unsigned int c1 = 0;
|
||||
unsigned int c2 = 0;
|
||||
unsigned int i = 0;
|
||||
unsigned int j = 0;
|
||||
unsigned int x = 0;
|
||||
unsigned int y = 0;
|
||||
int deltaRow, deltaCol; /* Change in row-column position based on key input */
|
||||
unsigned int score = 0;
|
||||
|
||||
|
||||
WINDOW* W = 0; /* Curses Window handle */
|
||||
unsigned int rowLimit,colLimit; /* Num of rows and columns in terminal window */
|
||||
/* Initialize ncurses -- you will want to use the following settings */
|
||||
|
||||
|
||||
|
||||
/* check to see if argument */
|
||||
if (argc != 2)
|
||||
{
|
||||
/*printw("An error has ocurred no input file was given.\n");*/
|
||||
return 1;
|
||||
}
|
||||
mazeFile = fopen(argv[1], "r");
|
||||
if (mazeFile == NULL)
|
||||
{
|
||||
/*printw("Error in opening the file.\n");*/
|
||||
return 1;
|
||||
}
|
||||
fgets(shold, 2, mazeFile);
|
||||
while ( feof(mazeFile) == 0 )
|
||||
{
|
||||
|
||||
|
||||
switch(shold[0])
|
||||
{
|
||||
case 'M':
|
||||
/* read array values, dynamically allocate array */
|
||||
fscanf(mazeFile, "%3u %3u", &row, &col);
|
||||
maze = (char**)malloc(row * 8); /* */
|
||||
for(i = (unsigned) 0; i < row; i++)
|
||||
{
|
||||
if (maze==NULL)
|
||||
{
|
||||
break;
|
||||
}
|
||||
maze[i] = malloc(col * 8);
|
||||
}
|
||||
for (x = (unsigned) 0; x < row; x++)
|
||||
{
|
||||
for (j = (unsigned) 0; j < col; j++)
|
||||
{
|
||||
fscanf(mazeFile, "%c", &maze[x][j]);
|
||||
if (maze[x][j] == '\n')
|
||||
{
|
||||
fscanf(mazeFile, "%c", &maze[x][j]);
|
||||
}
|
||||
}
|
||||
}
|
||||
break;
|
||||
case 'P':
|
||||
fscanf(mazeFile, "%3u %3u", &k, &l);
|
||||
/* check maze bounds */
|
||||
if(k>= row || l>= col){
|
||||
break;
|
||||
}
|
||||
maze[k][l] = 'P';
|
||||
break;
|
||||
|
||||
case 'Z':
|
||||
fscanf(mazeFile, "%3u %3u", &k, &l);
|
||||
/* check maze bounds */
|
||||
if(k>= row || l>= col){
|
||||
break;
|
||||
}
|
||||
maze[k][l] = 'Z';
|
||||
break;
|
||||
|
||||
case 'T':
|
||||
fscanf(mazeFile, "%3u %3u", &k, &l);
|
||||
/* check maze bounds */
|
||||
if(k>= row || l>= col){
|
||||
break;
|
||||
}
|
||||
maze[k][l] = 'T';
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
};
|
||||
fgets(shold, 2, mazeFile);
|
||||
if (feof(mazeFile) != 0)
|
||||
{
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
rowLimit = row;
|
||||
colLimit = col;
|
||||
fclose(mazeFile);
|
||||
/* LDRA_EXCLUDE 170 S <justification start> FALSE POSITIVE -- needed for ncurses <justification end> */
|
||||
/* LDRA_EXCLUDE 496 S <justification start> FALSE POSITIVE -- needed for ncurses <justification end> */
|
||||
/* LDRA_EXCLUDE 41 D <justification start> FALSE POSITIVE -- needed for ncurses <justification end> */
|
||||
W = initscr(); /* Determine terminal type and initialize curses data structures */
|
||||
/* LDRA_EXCLUDE 170 S <justification start> FALSE POSITIVE -- needed for ncurses <justification end> */
|
||||
/* LDRA_EXCLUDE 496 S <justification start> FALSE POSITIVE -- needed for ncurses <justification end> */
|
||||
/* LDRA_EXCLUDE 41 D <justification start> FALSE POSITIVE -- needed for ncurses <justification end> */
|
||||
start_color(); /* Enable use of color with ncurses */
|
||||
/* LDRA_EXCLUDE 170 S <justification start> FALSE POSITIVE -- needed for ncurses <justification end> */
|
||||
/* LDRA_EXCLUDE 496 S <justification start> FALSE POSITIVE -- needed for ncurses <justification end> */
|
||||
/* LDRA_EXCLUDE 41 D <justification start> FALSE POSITIVE -- needed for ncurses <justification end> */
|
||||
cbreak(); /* Disable line buffering and make char inputs immediately accessible to program */
|
||||
/* LDRA_EXCLUDE 170 S <justification start> FALSE POSITIVE -- needed for ncurses <justification end> */
|
||||
/* LDRA_EXCLUDE 496 S <justification start> FALSE POSITIVE -- needed for ncurses <justification end> */
|
||||
/* LDRA_EXCLUDE 41 D <justification start> FALSE POSITIVE -- needed for ncurses <justification end> */
|
||||
noecho(); /* Disable echo printing of chars type by user */
|
||||
if (W == NULL)
|
||||
{
|
||||
return 0;
|
||||
}
|
||||
/* LDRA_EXCLUDE 170 S <justification start> FALSE POSITIVE -- needed for ncurses <justification end> */
|
||||
/* LDRA_EXCLUDE 496 S <justification start> FALSE POSITIVE -- needed for ncurses <justification end> */
|
||||
/* LDRA_EXCLUDE 41 D <justification start> FALSE POSITIVE -- needed for ncurses <justification end> */
|
||||
nodelay(W, true); /* Make getch( ) a non-blocking call */
|
||||
/* LDRA_EXCLUDE 170 S <justification start> FALSE POSITIVE -- needed for ncurses <justification end> */
|
||||
/* LDRA_EXCLUDE 496 S <justification start> FALSE POSITIVE -- needed for ncurses <justification end> */
|
||||
/* LDRA_EXCLUDE 41 D <justification start> FALSE POSITIVE -- needed for ncurses <justification end> */
|
||||
keypad(W, true); /* Enable keypad and arrow keys */
|
||||
/* LDRA_EXCLUDE 170 S <justification start> FALSE POSITIVE -- needed for ncurses <justification end> */
|
||||
/* LDRA_EXCLUDE 496 S <justification start> FALSE POSITIVE -- needed for ncurses <justification end> */
|
||||
/* LDRA_EXCLUDE 41 D <justification start> FALSE POSITIVE -- needed for ncurses <justification end> */
|
||||
curs_set(0); /* Set cursor to be invisible - 0 */
|
||||
/* LDRA_EXCLUDE 170 S <justification start> FALSE POSITIVE -- needed for ncurses <justification end> */
|
||||
/* LDRA_EXCLUDE 496 S <justification start> FALSE POSITIVE -- needed for ncurses <justification end> */
|
||||
/* LDRA_EXCLUDE 41 D <justification start> FALSE POSITIVE -- needed for ncurses <justification end> */
|
||||
getmaxyx(W, rowLimit, colLimit); /* Query terminal dimensions */
|
||||
|
||||
/* Define color pallete foreground-background color pairs */
|
||||
/* PairID#, Foreground Color, Background Color */
|
||||
/* Foreground color == Background color ==> solid color block */
|
||||
/* LDRA_EXCLUDE 170 S <justification start> FALSE POSITIVE -- needed for ncurses <justification end> */
|
||||
/* LDRA_EXCLUDE 496 S <justification start> FALSE POSITIVE -- needed for ncurses <justification end> */
|
||||
/* LDRA_EXCLUDE 41 D <justification start> FALSE POSITIVE -- needed for ncurses <justification end> */
|
||||
init_pair(1, COLOR_BLACK, COLOR_BLACK); /* Black text on Black background */
|
||||
/* LDRA_EXCLUDE 170 S <justification start> FALSE POSITIVE -- needed for ncurses <justification end> */
|
||||
/* LDRA_EXCLUDE 496 S <justification start> FALSE POSITIVE -- needed for ncurses <justification end> */
|
||||
init_pair(2, COLOR_GREEN, COLOR_GREEN); /* Red text on Black background */
|
||||
/* LDRA_EXCLUDE 170 S <justification start> FALSE POSITIVE -- needed for ncurses <justification end> */
|
||||
/* LDRA_EXCLUDE 496 S <justification start> FALSE POSITIVE -- needed for ncurses <justification end> */
|
||||
init_pair(3, COLOR_WHITE, COLOR_WHITE); /* White text on Blue background */
|
||||
/* LDRA_EXCLUDE 170 S <justification start> FALSE POSITIVE -- needed for ncurses <justification end> */
|
||||
/* LDRA_EXCLUDE 496 S <justification start> FALSE POSITIVE -- needed for ncurses <justification end> */
|
||||
init_pair(4, COLOR_RED, COLOR_RED); /* Yellow text on Yellow background */
|
||||
/* LDRA_EXCLUDE 170 S <justification start> FALSE POSITIVE -- needed for ncurses <justification end> */
|
||||
/* LDRA_EXCLUDE 496 S <justification start> FALSE POSITIVE -- needed for ncurses <justification end> */
|
||||
init_pair(5, COLOR_YELLOW, COLOR_YELLOW); /* Yellow text on Yellow background */
|
||||
|
||||
|
||||
for (c1 =(unsigned)0; c1 < row; c1++)
|
||||
{
|
||||
/*printw ("\n");*/
|
||||
for (c2=(unsigned)0; c2 < col; c2++)
|
||||
{
|
||||
if (maze==NULL)
|
||||
{
|
||||
break;
|
||||
}
|
||||
switch(maze[c1][c2])
|
||||
{
|
||||
case '#':
|
||||
/* read array values, dynamically allocate array*/
|
||||
/* LDRA_EXCLUDE 170 S <justification start> FALSE POSITIVE -- needed for ncurses <justification end> */
|
||||
/* LDRA_EXCLUDE 170 S <justification start> FALSE POSITIVE -- needed for ncurses <justification end> */
|
||||
/* LDRA_EXCLUDE 496 S <justification start> FALSE POSITIVE -- needed for ncurses <justification end> */
|
||||
/* LDRA_EXCLUDE 496 S <justification start> FALSE POSITIVE -- needed for ncurses <justification end> */
|
||||
/* LDRA_EXCLUDE 41 D <justification start> FALSE POSITIVE -- needed for ncurses <justification end> */
|
||||
/* LDRA_EXCLUDE 41 D <justification start> FALSE POSITIVE -- needed for ncurses <justification end> */
|
||||
attron(COLOR_PAIR(2));
|
||||
/* LDRA_EXCLUDE 170 S <justification start> FALSE POSITIVE -- needed for ncurses <justification end> */
|
||||
/* LDRA_EXCLUDE 496 S <justification start> FALSE POSITIVE -- needed for ncurses <justification end> */
|
||||
wmove(W, c1,c2);
|
||||
/* LDRA_EXCLUDE 170 S <justification start> FALSE POSITIVE -- needed for ncurses <justification end> */
|
||||
/* LDRA_EXCLUDE 496 S <justification start> FALSE POSITIVE -- needed for ncurses <justification end> */
|
||||
waddch(W,'#');
|
||||
/* LDRA_EXCLUDE 170 S <justification start> FALSE POSITIVE -- needed for ncurses <justification end> */
|
||||
/* LDRA_EXCLUDE 170 S <justification start> FALSE POSITIVE -- needed for ncurses <justification end> */
|
||||
/* LDRA_EXCLUDE 496 S <justification start> FALSE POSITIVE -- needed for ncurses <justification end> */
|
||||
/* LDRA_EXCLUDE 496 S <justification start> FALSE POSITIVE -- needed for ncurses <justification end> */
|
||||
/* LDRA_EXCLUDE 41 D <justification start> FALSE POSITIVE -- needed for ncurses <justification end> */
|
||||
attroff(COLOR_PAIR(2));
|
||||
break;
|
||||
case ' ':
|
||||
/* LDRA_EXCLUDE 170 S <justification start> FALSE POSITIVE -- needed for ncurses <justification end> */
|
||||
/* LDRA_EXCLUDE 496 S <justification start> FALSE POSITIVE -- needed for ncurses <justification end> */
|
||||
wmove(W, c1,c2);
|
||||
/* LDRA_EXCLUDE 170 S <justification start> FALSE POSITIVE -- needed for ncurses <justification end> */
|
||||
/* LDRA_EXCLUDE 496 S <justification start> FALSE POSITIVE -- needed for ncurses <justification end> */
|
||||
waddch(W,' ');
|
||||
break;
|
||||
case 'P':
|
||||
/* LDRA_EXCLUDE 170 S <justification start> FALSE POSITIVE -- needed for ncurses <justification end> */
|
||||
/* LDRA_EXCLUDE 496 S <justification start> FALSE POSITIVE -- needed for ncurses <justification end> */
|
||||
wmove(W, c1,c2);
|
||||
/* LDRA_EXCLUDE 170 S <justification start> FALSE POSITIVE -- needed for ncurses <justification end> */
|
||||
/* LDRA_EXCLUDE 496 S <justification start> FALSE POSITIVE -- needed for ncurses <justification end> */
|
||||
waddch(W,'P');
|
||||
prow=c1;
|
||||
pcol=c2;
|
||||
break;
|
||||
case 'Z':
|
||||
/* LDRA_EXCLUDE 170 S <justification start> FALSE POSITIVE -- needed for ncurses <justification end> */
|
||||
/* LDRA_EXCLUDE 170 S <justification start> FALSE POSITIVE -- needed for ncurses <justification end> */
|
||||
/* LDRA_EXCLUDE 496 S <justification start> FALSE POSITIVE -- needed for ncurses <justification end> */
|
||||
/* LDRA_EXCLUDE 496 S <justification start> FALSE POSITIVE -- needed for ncurses <justification end> */
|
||||
attron(COLOR_PAIR(4));
|
||||
/* LDRA_EXCLUDE 170 S <justification start> FALSE POSITIVE -- needed for ncurses <justification end> */
|
||||
/* LDRA_EXCLUDE 496 S <justification start> FALSE POSITIVE -- needed for ncurses <justification end> */
|
||||
wmove(W, c1,c2);
|
||||
/* LDRA_EXCLUDE 170 S <justification start> FALSE POSITIVE -- needed for ncurses <justification end> */
|
||||
/* LDRA_EXCLUDE 496 S <justification start> FALSE POSITIVE -- needed for ncurses <justification end> */
|
||||
waddch(W,'Z');
|
||||
/* LDRA_EXCLUDE 170 S <justification start> FALSE POSITIVE -- needed for ncurses <justification end> */
|
||||
/* LDRA_EXCLUDE 170 S <justification start> FALSE POSITIVE -- needed for ncurses <justification end> */
|
||||
/* LDRA_EXCLUDE 496 S <justification start> FALSE POSITIVE -- needed for ncurses <justification end> */
|
||||
/* LDRA_EXCLUDE 496 S <justification start> FALSE POSITIVE -- needed for ncurses <justification end> */
|
||||
attroff(COLOR_PAIR(4));
|
||||
break;
|
||||
case 'T':
|
||||
/* LDRA_EXCLUDE 170 S <justification start> FALSE POSITIVE -- needed for ncurses <justification end> */
|
||||
/* LDRA_EXCLUDE 170 S <justification start> FALSE POSITIVE -- needed for ncurses <justification end> */
|
||||
/* LDRA_EXCLUDE 496 S <justification start> FALSE POSITIVE -- needed for ncurses <justification end> */
|
||||
/* LDRA_EXCLUDE 496 S <justification start> FALSE POSITIVE -- needed for ncurses <justification end> */
|
||||
attron(COLOR_PAIR(5));
|
||||
/* LDRA_EXCLUDE 170 S <justification start> FALSE POSITIVE -- needed for ncurses <justification end> */
|
||||
/* LDRA_EXCLUDE 496 S <justification start> FALSE POSITIVE -- needed for ncurses <justification end> */
|
||||
wmove(W, c1,c2);
|
||||
/* LDRA_EXCLUDE 170 S <justification start> FALSE POSITIVE -- needed for ncurses <justification end> */
|
||||
/* LDRA_EXCLUDE 496 S <justification start> FALSE POSITIVE -- needed for ncurses <justification end> */
|
||||
waddch(W,'T');
|
||||
/* LDRA_EXCLUDE 170 S <justification start> FALSE POSITIVE -- needed for ncurses <justification end> */
|
||||
/* LDRA_EXCLUDE 170 S <justification start> FALSE POSITIVE -- needed for ncurses <justification end> */
|
||||
/* LDRA_EXCLUDE 496 S <justification start> FALSE POSITIVE -- needed for ncurses <justification end> */
|
||||
/* LDRA_EXCLUDE 496 S <justification start> FALSE POSITIVE -- needed for ncurses <justification end> */
|
||||
attroff(COLOR_PAIR(5));
|
||||
break;
|
||||
|
||||
default:
|
||||
break;
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
deltaRow =0;
|
||||
deltaCol =0;
|
||||
|
||||
|
||||
|
||||
do{
|
||||
|
||||
/* LDRA_EXCLUDE 170 S <justification start> FALSE POSITIVE -- needed for ncurses <justification end> */
|
||||
/* LDRA_EXCLUDE 496 S <justification start> FALSE POSITIVE -- needed for ncurses <justification end> */
|
||||
int kb = wgetch(W); /* Grab keypress */
|
||||
|
||||
/* Support player movement via WASD, IJKL, and arrow keys */
|
||||
if (kb == (int)'a' || kb == (int)'j' || kb == KEY_LEFT)
|
||||
{
|
||||
/* Move Left */
|
||||
deltaRow =0;
|
||||
deltaCol =-1;
|
||||
}
|
||||
else if (kb == (int)'d' || kb == (int)'l' || kb == KEY_RIGHT)
|
||||
{
|
||||
/* Move Right */
|
||||
deltaRow =0;
|
||||
deltaCol =1;
|
||||
}
|
||||
else if (kb == (int)'w' || kb == (int)'i' || kb == KEY_UP)
|
||||
{
|
||||
/* Move Up */
|
||||
deltaRow =-1;
|
||||
deltaCol =0;
|
||||
}
|
||||
else if (kb == (int)'s' || kb == (int)'k' || kb == KEY_DOWN)
|
||||
{
|
||||
/* Move Down */
|
||||
deltaRow =1;
|
||||
deltaCol =0;
|
||||
}
|
||||
else if (kb == (int)'q')
|
||||
{
|
||||
/* q to exit player movement loop */
|
||||
break;
|
||||
};
|
||||
|
||||
/* LDRA_EXCLUDE 170 S <justification start> FALSE POSITIVE -- needed for ncurses <justification end> */
|
||||
/* LDRA_EXCLUDE 170 S <justification start> FALSE POSITIVE -- needed for ncurses <justification end> */
|
||||
/* LDRA_EXCLUDE 496 S <justification start> FALSE POSITIVE -- needed for ncurses <justification end> */
|
||||
/* LDRA_EXCLUDE 496 S <justification start> FALSE POSITIVE -- needed for ncurses <justification end> */
|
||||
attron(COLOR_PAIR(1));
|
||||
/* LDRA_EXCLUDE 170 S <justification start> FALSE POSITIVE -- needed for ncurses <justification end> */
|
||||
/* LDRA_EXCLUDE 496 S <justification start> FALSE POSITIVE -- needed for ncurses <justification end> */
|
||||
wmove(W,prow,pcol);
|
||||
/* LDRA_EXCLUDE 170 S <justification start> FALSE POSITIVE -- needed for ncurses <justification end> */
|
||||
/* LDRA_EXCLUDE 496 S <justification start> FALSE POSITIVE -- needed for ncurses <justification end> */
|
||||
waddch(W, 'P');
|
||||
/* LDRA_EXCLUDE 170 S <justification start> FALSE POSITIVE -- needed for ncurses <justification end> */
|
||||
/* LDRA_EXCLUDE 170 S <justification start> FALSE POSITIVE -- needed for ncurses <justification end> */
|
||||
/* LDRA_EXCLUDE 496 S <justification start> FALSE POSITIVE -- needed for ncurses <justification end> */
|
||||
/* LDRA_EXCLUDE 496 S <justification start> FALSE POSITIVE -- needed for ncurses <justification end> */
|
||||
attroff(COLOR_PAIR(1));
|
||||
|
||||
if(((prow + (unsigned int)deltaRow) > row) || ((pcol + (unsigned int)deltaCol) > col)){
|
||||
deltaRow = 0;
|
||||
deltaCol = 0;
|
||||
}
|
||||
if(maze[((int)prow + deltaRow)][((int)pcol + deltaCol)] == '#'){
|
||||
prow = prow;
|
||||
pcol = pcol;
|
||||
}
|
||||
else{
|
||||
prow = prow + (unsigned int)deltaRow;
|
||||
pcol = pcol + (unsigned int)deltaCol;
|
||||
}
|
||||
if(maze[prow][pcol] == 'T')
|
||||
{
|
||||
maze[prow][pcol] = ' ';
|
||||
score++;
|
||||
}
|
||||
if(maze[prow][pcol] == 'Z')
|
||||
{
|
||||
break;
|
||||
}
|
||||
|
||||
/* LDRA_EXCLUDE 170 S <justification start> FALSE POSITIVE -- needed for ncurses <justification end> */
|
||||
/* LDRA_EXCLUDE 170 S <justification start> FALSE POSITIVE -- needed for ncurses <justification end> */
|
||||
/* LDRA_EXCLUDE 496 S <justification start> FALSE POSITIVE -- needed for ncurses <justification end> */
|
||||
/* LDRA_EXCLUDE 496 S <justification start> FALSE POSITIVE -- needed for ncurses <justification end> */
|
||||
attron(COLOR_PAIR(3));
|
||||
/* LDRA_EXCLUDE 170 S <justification start> FALSE POSITIVE -- needed for ncurses <justification end> */
|
||||
/* LDRA_EXCLUDE 496 S <justification start> FALSE POSITIVE -- needed for ncurses <justification end> */
|
||||
wmove(W,prow,pcol);
|
||||
/* LDRA_EXCLUDE 170 S <justification start> FALSE POSITIVE -- needed for ncurses <justification end> */
|
||||
/* LDRA_EXCLUDE 496 S <justification start> FALSE POSITIVE -- needed for ncurses <justification end> */
|
||||
waddch(W, 'P');
|
||||
/* LDRA_EXCLUDE 170 S <justification start> FALSE POSITIVE -- needed for ncurses <justification end> */
|
||||
/* LDRA_EXCLUDE 170 S <justification start> FALSE POSITIVE -- needed for ncurses <justification end> */
|
||||
/* LDRA_EXCLUDE 496 S <justification start> FALSE POSITIVE -- needed for ncurses <justification end> */
|
||||
/* LDRA_EXCLUDE 496 S <justification start> FALSE POSITIVE -- needed for ncurses <justification end> */
|
||||
attroff(COLOR_PAIR(3));
|
||||
|
||||
deltaRow =0;
|
||||
deltaCol =0;
|
||||
|
||||
/* LDRA_EXCLUDE 170 S <justification start> FALSE POSITIVE -- needed for ncurses <justification end> */
|
||||
/* LDRA_EXCLUDE 496 S <justification start> FALSE POSITIVE -- needed for ncurses <justification end> */
|
||||
wrefresh(W); /* Refresh ncurses window */
|
||||
|
||||
} while (true); /* Repeat until user selects q to quit */
|
||||
|
||||
/* free the memory */
|
||||
for(y = (unsigned)0; y < row; y++)
|
||||
{
|
||||
free(maze[y]);
|
||||
maze[y] = NULL;
|
||||
}
|
||||
if(maze != NULL){
|
||||
free(maze);
|
||||
}
|
||||
/*printw("\n");*/
|
||||
/* LDRA_EXCLUDE 170 S <justification start> FALSE POSITIVE -- needed for ncurses <justification end> */
|
||||
/* LDRA_EXCLUDE 496 S <justification start> FALSE POSITIVE -- needed for ncurses <justification end> */
|
||||
/* LDRA_EXCLUDE 41 D <justification start> FALSE POSITIVE -- needed for ncurses <justification end> */
|
||||
endwin();
|
||||
printf("Score = %u\n", score);
|
||||
return 0;
|
||||
}
|
||||
|
@ -0,0 +1,124 @@
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <stdint.h>
|
||||
#include <string.h>
|
||||
|
||||
|
||||
|
||||
|
||||
int main(int argc, char *argv[])
|
||||
{
|
||||
FILE* mazeFile; /* Maze Input File */
|
||||
char chold = NULL; /* Holds character for line input from file */
|
||||
unsigned int row = 0; /* Holds number of rows for input from file */
|
||||
unsigned int col = 0; /* Holds number of cols for input from file */
|
||||
char shold[2000]; /* Holds lines for input from file */
|
||||
unsigned int l = 0;
|
||||
unsigned int k = 0;
|
||||
char **maze; // global array for maze
|
||||
unsigned int c1 = 0;
|
||||
unsigned int c2 = 0;
|
||||
|
||||
// check to see if
|
||||
if (argc != 2)
|
||||
{
|
||||
printf("An error has ocurred no input file was given.\n");
|
||||
return 1;
|
||||
}
|
||||
mazeFile = fopen(argv[1], "r");
|
||||
if (mazeFile == NULL)
|
||||
{
|
||||
printf("Error in opening the file.\n");
|
||||
return 1;
|
||||
}
|
||||
fgets(shold, 2, mazeFile);
|
||||
while ( feof(mazeFile) == 0 )
|
||||
{
|
||||
|
||||
switch(shold[0])
|
||||
{
|
||||
case 'M':
|
||||
fscanf(mazeFile, "%3d %3d", &row, &col);
|
||||
// dynamic allocation of array for maze
|
||||
unsigned int i = 0, j = 0;
|
||||
maze = (char**)malloc(row * sizeof(unsigned int *));
|
||||
for(i = (unsigned) 0; i < row; i++)
|
||||
{
|
||||
if (maze==NULL)
|
||||
{
|
||||
break;
|
||||
}
|
||||
maze[i] = (char*)malloc(col * sizeof(unsigned int *));
|
||||
}
|
||||
for (i = (unsigned) 0; i < row; i++)
|
||||
{
|
||||
for (j = (unsigned) 0; j < col; j++)
|
||||
{
|
||||
fscanf(mazeFile, "%c", &maze[i][j]);
|
||||
if (maze[i][j] == '\n')
|
||||
{
|
||||
fscanf(mazeFile, "%c", &maze[i][j]);
|
||||
}
|
||||
}
|
||||
}
|
||||
break;
|
||||
case 'P':
|
||||
fscanf(mazeFile, "%3d %3d", &k, &l);
|
||||
if(k>= row || l>= col){
|
||||
break;
|
||||
}
|
||||
maze[k][l] = 'P';
|
||||
break;
|
||||
|
||||
case 'Z':
|
||||
fscanf(mazeFile, "%3d %3d", &k, &l);
|
||||
if(k>= row || l>= col){
|
||||
break;
|
||||
}
|
||||
maze[k][l] = 'Z';
|
||||
break;
|
||||
|
||||
case 'T':
|
||||
fscanf(mazeFile, "%3d %3d", &k, &l);
|
||||
if(k>= row || l>= col){
|
||||
break;
|
||||
}
|
||||
maze[k][l] = 'T';
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
};
|
||||
fgets(shold, 2, mazeFile);
|
||||
if (feof(mazeFile) != 0)
|
||||
{
|
||||
break;
|
||||
}
|
||||
}
|
||||
fclose(mazeFile);
|
||||
for (c1 =(unsigned)0; c1 < row; c1++)
|
||||
{
|
||||
printf ("\n");
|
||||
for (c2=(unsigned)0; c2 < col; c2++)
|
||||
{
|
||||
if (maze==NULL)
|
||||
{
|
||||
break;
|
||||
}
|
||||
printf("%c", maze[c1][c2]);
|
||||
}
|
||||
}
|
||||
|
||||
//free the memory
|
||||
unsigned int i = 0;
|
||||
for(i = (unsigned)0; i < row; i++)
|
||||
{
|
||||
free(maze[i]);
|
||||
maze[i] = NULL;
|
||||
}
|
||||
if(maze != NULL){
|
||||
free(maze);
|
||||
}
|
||||
printf("\n");
|
||||
|
||||
return 0;
|
||||
}
|
@ -0,0 +1,128 @@
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <stdint.h>
|
||||
#include <string.h>
|
||||
#include <ncurses.h>
|
||||
|
||||
|
||||
|
||||
|
||||
int main(int argc, char *argv[])
|
||||
{
|
||||
FILE* mazeFile; /* Maze Input File */
|
||||
unsigned int row = 0; /* Holds number of rows for input from file */
|
||||
unsigned int col = 0; /* Holds number of cols for input from file */
|
||||
char shold[2000]; /* Holds lines for input from file */
|
||||
unsigned int l = 0;
|
||||
unsigned int k = 0;
|
||||
char **maze; /* array for maze */
|
||||
unsigned int c1 = 0;
|
||||
unsigned int c2 = 0;
|
||||
|
||||
/* check to see if argument */
|
||||
if (argc != 2)
|
||||
{
|
||||
printf("An error has ocurred no input file was given.\n");
|
||||
return 1;
|
||||
}
|
||||
mazeFile = fopen(argv[1], "r");
|
||||
if (mazeFile == NULL)
|
||||
{
|
||||
printf("Error in opening the file.\n");
|
||||
return 1;
|
||||
}
|
||||
fgets(shold, 2, mazeFile);
|
||||
while ( feof(mazeFile) == 0 )
|
||||
{
|
||||
|
||||
switch(shold[0])
|
||||
{
|
||||
case 'M':
|
||||
/* read array values, dynamically allocate array */
|
||||
fscanf(mazeFile, "%3u %3u", &row, &col);
|
||||
unsigned int i = 0, j = 0;
|
||||
maze = (char**)malloc(row * 8);
|
||||
for(i = (unsigned) 0; i < row; i++)
|
||||
{
|
||||
if (maze==NULL)
|
||||
{
|
||||
break;
|
||||
}
|
||||
maze[i] = malloc(col * 8);
|
||||
}
|
||||
for (i = (unsigned) 0; i < row; i++)
|
||||
{
|
||||
for (j = (unsigned) 0; j < col; j++)
|
||||
{
|
||||
fscanf(mazeFile, "%c", &maze[i][j]);
|
||||
if (maze[i][j] == '\n')
|
||||
{
|
||||
fscanf(mazeFile, "%c", &maze[i][j]);
|
||||
}
|
||||
}
|
||||
}
|
||||
break;
|
||||
case 'P':
|
||||
fscanf(mazeFile, "%3u %3u", &k, &l);
|
||||
/* check maze bounds */
|
||||
if(k>= row || l>= col){
|
||||
break;
|
||||
}
|
||||
maze[k][l] = 'P';
|
||||
break;
|
||||
|
||||
case 'Z':
|
||||
fscanf(mazeFile, "%3u %3u", &k, &l);
|
||||
/* check maze bounds */
|
||||
if(k>= row || l>= col){
|
||||
break;
|
||||
}
|
||||
maze[k][l] = 'Z';
|
||||
break;
|
||||
|
||||
case 'T':
|
||||
fscanf(mazeFile, "%3u %3u", &k, &l);
|
||||
/* check maze bounds */
|
||||
if(k>= row || l>= col){
|
||||
break;
|
||||
}
|
||||
maze[k][l] = 'T';
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
};
|
||||
fgets(shold, 2, mazeFile);
|
||||
if (feof(mazeFile) != 0)
|
||||
{
|
||||
break;
|
||||
}
|
||||
}
|
||||
fclose(mazeFile);
|
||||
/* print out maze */
|
||||
for (c1 =(unsigned)0; c1 < row; c1++)
|
||||
{
|
||||
printf ("\n");
|
||||
for (c2=(unsigned)0; c2 < col; c2++)
|
||||
{
|
||||
if (maze==NULL)
|
||||
{
|
||||
break;
|
||||
}
|
||||
printf("%c", maze[c1][c2]);
|
||||
}
|
||||
}
|
||||
|
||||
/* free the memory */
|
||||
unsigned int i = 0;
|
||||
for(i = (unsigned)0; i < row; i++)
|
||||
{
|
||||
free(maze[i]);
|
||||
maze[i] = NULL;
|
||||
}
|
||||
if(maze != NULL){
|
||||
free(maze);
|
||||
}
|
||||
printf("\n");
|
||||
|
||||
return 0;
|
||||
}
|
@ -0,0 +1,134 @@
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <stdint.h>
|
||||
#include <string.h>
|
||||
#include <unistd.h>
|
||||
#include <ncurses.h>
|
||||
|
||||
|
||||
|
||||
|
||||
int main(int argc, char *argv[])
|
||||
{
|
||||
FILE* mazeFile; /* Maze Input File */
|
||||
unsigned int row = 0; /* Holds number of rows for input from file */
|
||||
unsigned int col = 0; /* Holds number of cols for input from file */
|
||||
char shold[2000]; /* Holds lines for input from file */
|
||||
unsigned int l = 0;
|
||||
unsigned int k = 0;
|
||||
char **maze; /* array for maze */
|
||||
unsigned int c1 = 0;
|
||||
unsigned int c2 = 0;
|
||||
unsigned int i = 0;
|
||||
unsigned int j = 0;
|
||||
unsigned int x = 0;
|
||||
unsigned int y = 0;
|
||||
WINDOW* W = 0; /* Curses Window handle */
|
||||
|
||||
/* check to see if argument */
|
||||
if (argc != 2)
|
||||
{
|
||||
/*printw("An error has ocurred no input file was given.\n");*/
|
||||
return 1;
|
||||
}
|
||||
mazeFile = fopen(argv[1], "r");
|
||||
if (mazeFile == NULL)
|
||||
{
|
||||
/*printw("Error in opening the file.\n");*/
|
||||
return 1;
|
||||
}
|
||||
fgets(shold, 2, mazeFile);
|
||||
while ( feof(mazeFile) == 0 )
|
||||
{
|
||||
|
||||
switch(shold[0])
|
||||
{
|
||||
case 'M':
|
||||
/* read array values, dynamically allocate array */
|
||||
fscanf(mazeFile, "%3u %3u", &row, &col);
|
||||
maze = (char**)malloc(row * 8);
|
||||
for(i = (unsigned) 0; i < row; i++)
|
||||
{
|
||||
if (maze==NULL)
|
||||
{
|
||||
break;
|
||||
}
|
||||
maze[i] = malloc(col * 8);
|
||||
}
|
||||
for (x = (unsigned) 0; x < row; x++)
|
||||
{
|
||||
for (j = (unsigned) 0; j < col; j++)
|
||||
{
|
||||
fscanf(mazeFile, "%c", &maze[x][j]);
|
||||
if (maze[x][j] == '\n')
|
||||
{
|
||||
fscanf(mazeFile, "%c", &maze[x][j]);
|
||||
}
|
||||
}
|
||||
}
|
||||
break;
|
||||
case 'P':
|
||||
fscanf(mazeFile, "%3u %3u", &k, &l);
|
||||
/* check maze bounds */
|
||||
if(k>= row || l>= col){
|
||||
break;
|
||||
}
|
||||
maze[k][l] = 'P';
|
||||
break;
|
||||
|
||||
case 'Z':
|
||||
fscanf(mazeFile, "%3u %3u", &k, &l);
|
||||
/* check maze bounds */
|
||||
if(k>= row || l>= col){
|
||||
break;
|
||||
}
|
||||
maze[k][l] = 'Z';
|
||||
break;
|
||||
|
||||
case 'T':
|
||||
fscanf(mazeFile, "%3u %3u", &k, &l);
|
||||
/* check maze bounds */
|
||||
if(k>= row || l>= col){
|
||||
break;
|
||||
}
|
||||
maze[k][l] = 'T';
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
};
|
||||
fgets(shold, 2, mazeFile);
|
||||
if (feof(mazeFile) != 0)
|
||||
{
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
fclose(mazeFile);
|
||||
|
||||
|
||||
for (c1 =(unsigned)0; c1 < row; c1++)
|
||||
{
|
||||
/*printw ("\n");*/
|
||||
for (c2=(unsigned)0; c2 < col; c2++)
|
||||
{
|
||||
if (maze==NULL)
|
||||
{
|
||||
break;
|
||||
}
|
||||
/*printw("%c", maze[c1][c2]);*/
|
||||
}
|
||||
}
|
||||
|
||||
/* free the memory */
|
||||
for(y = (unsigned)0; y < row; y++)
|
||||
{
|
||||
free(maze[y]);
|
||||
maze[y] = NULL;
|
||||
}
|
||||
if(maze != NULL){
|
||||
free(maze);
|
||||
}
|
||||
/*printw("\n");*/
|
||||
return 0;
|
||||
}
|
@ -0,0 +1,176 @@
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <stdint.h>
|
||||
#include <string.h>
|
||||
|
||||
void readPlayer(char *arr);
|
||||
void readZombie(char *arr);
|
||||
void readTreasure(char *arr);
|
||||
|
||||
// global array for maze
|
||||
char **maze;
|
||||
|
||||
// read maze file in to the array
|
||||
void readMaze(char *arr, int r, int c);
|
||||
|
||||
int main(int argc, char *argv[])
|
||||
{
|
||||
FILE* mazeFile; /* Maze Input File */
|
||||
char chold; /* Holds character for line input from file */
|
||||
int row; /* Holds number of rows for input from file */
|
||||
int col; /* Holds number of cols for input from file */
|
||||
char shold[2000]; /* Holds lines for input from file */
|
||||
int l;
|
||||
int k;
|
||||
int c1;
|
||||
int c2;
|
||||
|
||||
if (argc != 2)
|
||||
{
|
||||
printf("An error has ocurred no input file was given.\n");
|
||||
return 1;
|
||||
}
|
||||
mazeFile = fopen(argv[1], "r");
|
||||
if (mazeFile == NULL)
|
||||
{
|
||||
printf("Error in opening the file.\n");
|
||||
return 1;
|
||||
}
|
||||
fgets(shold, 2, mazeFile);
|
||||
while ( !feof(mazeFile) )
|
||||
{
|
||||
|
||||
switch(shold[0])
|
||||
{
|
||||
case 'M':
|
||||
//printf ("Maze encountered\n");
|
||||
fscanf(mazeFile, "%d %d", &row, &col);
|
||||
// printf("%d\n", row);
|
||||
// possible dynamic allocation of 2D array
|
||||
// dynamic allocation of array for maze
|
||||
int i = 0, j = 0;
|
||||
maze = malloc(row * sizeof(char *));
|
||||
for(i = 0; i < row; i++)
|
||||
{
|
||||
maze[i] = malloc(col * sizeof(char));
|
||||
}
|
||||
for (i = 0; i < row; i++)
|
||||
{
|
||||
for (j = 0; j < col; j++)
|
||||
{
|
||||
//fscanf(mazeFile, "%c", &chold);
|
||||
fscanf(mazeFile, "%c", &maze[i][j]);
|
||||
if (maze[i][j] == '\n')
|
||||
{
|
||||
printf ("\n");
|
||||
fscanf(mazeFile, "%c", &maze[i][j]);
|
||||
}
|
||||
printf("%c", maze[i][j]);
|
||||
}
|
||||
|
||||
}
|
||||
// printf("%c", maze[0 * col + 0]);
|
||||
// for(i = 0; i < row; i++)
|
||||
//{
|
||||
// free(maze[i]);
|
||||
//}
|
||||
// free(maze);
|
||||
// int (*maze)[col] = malloc(sizeof(int[row][col]));
|
||||
//readMaze(maze, row, col);
|
||||
break;
|
||||
case 'P':
|
||||
//case for player
|
||||
//printf ("Player\n");
|
||||
fscanf(mazeFile, "%d %d", &k, &l);
|
||||
maze[k][l] = 'P';
|
||||
printf("%c" , maze[k][l]);
|
||||
//printf("%d\n", row);
|
||||
// possible dynamic allocation of 2D array
|
||||
// char* player = malloc((row * col) * sizeof(char));
|
||||
// readPlayer(player);
|
||||
break;
|
||||
|
||||
case 'Z':
|
||||
//case for zombie
|
||||
printf ("Zombie\n");
|
||||
fscanf(mazeFile, "%d %d", &row, &col);
|
||||
printf("%d\n", row);
|
||||
// possible dynamic allocation of 2D array
|
||||
// char* zombie = malloc((row * col) * sizeof(char));
|
||||
// readZombie(zombie);
|
||||
break;
|
||||
|
||||
case 'T':
|
||||
//case for treasure
|
||||
printf ("Treasure\n");
|
||||
fscanf(mazeFile, "%d %d", &row, &col);
|
||||
printf("%d\n", row);
|
||||
// possible dynamic allocation of 2D array
|
||||
// char* treasure = malloc((row * col) * sizeof(char));
|
||||
// readTreasure(treasure);
|
||||
break;
|
||||
case '\n':
|
||||
printf("\n");
|
||||
break;
|
||||
default:
|
||||
//printf ("Basic error\n");
|
||||
break;
|
||||
};
|
||||
|
||||
// for(int i=0;i<row;i++)
|
||||
// {
|
||||
// for(int j=0;j<col;j++)
|
||||
// {
|
||||
// if(fscanf == #) //if file has #
|
||||
// {
|
||||
// maze[i][j] = #;
|
||||
// }
|
||||
// else
|
||||
// {
|
||||
// maze[i][j] = " ";
|
||||
// }
|
||||
// }
|
||||
//}
|
||||
|
||||
fgets(shold, 2, mazeFile);
|
||||
if (feof(mazeFile))
|
||||
{
|
||||
break;
|
||||
}
|
||||
}
|
||||
fclose(mazeFile);
|
||||
for (c1 =0; c1 < row; c1++)
|
||||
{
|
||||
printf ("\n");
|
||||
for (c2=0; c2 < col; c2++)
|
||||
{
|
||||
printf("%c", maze[c1][c2]);
|
||||
}
|
||||
}
|
||||
//printf ("So this line won't print without an argument.\n");
|
||||
|
||||
//free the memory
|
||||
// free(maze);
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
void readMaze(char *arr, int row, int col)
|
||||
{
|
||||
|
||||
|
||||
}
|
||||
|
||||
void readPlayer(char *arr)
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
void readZombie(char *arr)
|
||||
{
|
||||
|
||||
}
|
||||
void readTreasure(char *arr)
|
||||
{
|
||||
|
||||
}
|
Some files were not shown because too many files have changed in this diff Show More
Reference in New Issue
Block a user