Creating Pac-man scheme/racket coding - Vector conversion - vector

Well, I'm trying to do a maze-game, Pac-man, I need creation and render of map, for this, I did a "map of vectors" with the different states that I need.
This is the code:
#lang racket
(require 2htdp/image 2htdp/universe
(only-in racket/draw
read-bitmap))
(require math/matrix)
(require math/array)
(define CELL-SIZE 20)
(define E (rectangle CELL-SIZE CELL-SIZE "solid" "black"))
(define D (circle 3 "solid" "YELLOW"))
(define W (rectangle CELL-SIZE CELL-SIZE "solid" "blue"))
(define C (rectangle CELL-SIZE CELL-SIZE "solid" "red"))
(define Initial-Map
(vector (vector W W W W W W W W W W W W W)
(vector W C D D D D D D D D D D W)
(vector W D W D W W W W W D W D W)
(vector W D W D W D D D W D W D W)
(vector W D W D D D W D D D W D W)
(vector W D W W D W W W D W W D W)
(vector E D D D D D E D D D D D E)
(vector W D W W D W W W D W W D W)
(vector W D W D D D W D D D W D W)
(vector W D W D W D W D W D W D W)
(vector W D W D D D W D D D W D W)
(vector W D W D W D D D W D W D W)
(vector W D D C W D W D W D D D W)
(vector W W W W W W W W W W W W W)))
(define EMPTY (rectangle CELL-SIZE CELL-SIZE "solid" "black")) ; Empty
(define DOT (circle 3 "solid" "white")) ; DOT
(define WALL (rectangle CELL-SIZE CELL-SIZE "solid" "blue")) ; Wall
(define Scene (empty-scene 400 400 "black"))
(define (paintWorld mundo) (place-image (vector*->matrix Initial-Map) 100 100 Scene))
(big-bang 4
;(on-key changePacmanM)
;(on-tick automaticMovePacman 0.2)
(to-draw paintWorld)
(state true)
(name "Pacman - Racket"))
This is the problem
Initial-Map is the map vector.
How can i render it for the big-bang creation universe function?
I tried to convert vector to matrix with vector*->matrix in order to pass it to place-image:
(place-image (vector*->matrix Initial-Map) 100 100 Scene)
But I get the following error:
place-image: expects an image as first argument, given (mutable-array #[#[(object:image% ...) (object:image% ...) (object:image% ...) (object:image% ...) (object:image% ...)

Related

Generating multiple bar plots in multiple columns concurrently in R

Data are as follows:
df<-read.table(text=" A1 A2 A3 M1 M2 M3
F M F A B A
M M F A B A
F M F A B A
F M F C B A
F M F C B A
M M F C C B
F M F C C B
M F F C C B
F F F D C B
M F F D C B
F F F D A B
F F F D A C
F F F D A C
M F F D A C
F M M B A D
F M M B A D
F M M B D D
M M M B D D
F M M B D D ", h=T)
I want to have bar plots for A1 with M1; A2 with M2 and A3 with M3. So far I've tried:
library(purrr)
library(ggplot2)
map2(names(df)[4:6], names(df)[1:3], ~
ggplot(df, aes(x = !!rlang::sym(.x), y = !!rlang::sym(.y))) +
geom_bar())
However, I get the following error:
Error: stat_count() must not be used with a y aesthetic.
I struggled to fix the error. Any help?
I want to have this for each plot:
ggplot(df, aes(x = A1, fill = M1)) +
geom_bar(position = position_dodge())
Combining both the working example and what was tried, did you want:
library(purrr)
library(ggplot2)
map2(names(df)[1:3], names(df)[4:6], ~
ggplot(df, aes_string(x = .x, fill = .y)) +
geom_bar(position = position_dodge()))
It looks like you want fill for columns 4-6 (M1, M2, and M3) - is that correct? You did not need y in aes then, just fill.
Also, you can make use of aes_string instead of rlang::sym.
Finally, added position_dodge based on your working example. Let me know if this is what you had in mind.

How do I change the color of points on my graph that are attached to a factor?

I have the following function that I am using to try and create a graph that shows the different sexes as well as the difference in length and weight of fish.
ggplot(fish1,aes(x=Weight...,y=Fork.Length...,col = Sex))+geom_point()
However, I have been told that using red and green in a plot is not advisable due to colorblind people. How can I change what colors R assigns to each of my factors. Since this is an scatter-plot I cannot just do a col = c("red","yellow" "blue") as that list would have to be 132 factors long.
Here is my list of Sex factors:
# [1] M M M M M M M U U M F F F M F M F F M
# [20] F M M M M F F F M M M M M M F M M M M
# [39] M M F F M M F M M M M M F M F F F M F
# [58] F F F F F F M F M F U F M U F M M F F
# [77] M F U M M M M M U M F U F F F U M F M
# [96] F F U U F F M F M M F F M F F F M F M
# [115] F M F M M M F M F F F F F F M F M F

Whole dataset shows up, although a subset has been selected and newly defined

I a dataframe which I have subsetted using normal indexing. Code below.
dframe <- dframe[1:10, c(-3,-7:-10)]
But when I write dframe$Symbol I get the output.
BABA ORCL LFC TSM ACT ABBV MA ABEV KMI UPS
3285 Levels: A AA AA^B AAC AAN AAP AAT AAV AB ABB ABBV ABC ABEV ABG ABM ABR ABR^A ABR^B ABR^C ABRN ABT ABX ACC ACCO ACE ACG ACH ACI ACM ACN ACP ACRE ACT ACT^A ACW ADC ADM ADPT ADS ADT ADX AEB AEC AED AEE AEG AEH AEK AEL AEM AEO AEP AER AES AES^C AET AF AF^C ... ZX
I'm wondering what is happening here. Does the dframe dataframe only contain 10 rows or still all rows, but only outputs 10 rows?
Thanks
That's just the way factors work. When you subset a factor, it preserves all levels, even those that are no longer represented in the subset. For example:
f1 <- factor(letters);
f1;
## [1] a b c d e f g h i j k l m n o p q r s t u v w x y z
## Levels: a b c d e f g h i j k l m n o p q r s t u v w x y z
f2 <- f1[1:10];
f2;
## [1] a b c d e f g h i j
## Levels: a b c d e f g h i j k l m n o p q r s t u v w x y z
To answer your question, it's actually slightly tricky to append all missing levels to a factor. You have to combine the existing factor data with all missing indexes (here I'm referring to the integer indexes that the factor class internally uses to map the actual factor data to its levels vector, which is stored as an attribute on the factor object), and then rebuild a factor (using the original levels) from that combined data. Below I demonstrate this, now randomizing the subset taken from f1 to demonstrate that order does not matter:
set.seed(1); f3 <- sample(f1,10);
f3;
## [1] g j n u e s w m l b
## Levels: a b c d e f g h i j k l m n o p q r s t u v w x y z
factor(c(f3,setdiff(1:nlevels(f3),as.integer(f3))),labels=levels(f3));
## [1] g j n u e s w m l b a c d f h i k o p q r t v x y z
## Levels: a b c d e f g h i j k l m n o p q r s t u v w x y z

Print a chessboard without using loops, is it possible?

Like this: (Ideally in Java, Scala, C# but any well established computer language will do as well) so not pseudo code:
B W B W B W B W
W B W B W B W B
B W B W B W B W
W B W B W B W B
B W B W B W B W
W B W B W B W B
B W B W B W B W
W B W B W B W B
Techincally, I assume, it is quite easy (pseudo code):
function PrintFields(int index) {
int x=index % 8;
int y=index / 8;
int bw=index % 2;
print_at(x,y,(bw==0)?'B':'W');
if (index<63) PrintFields(index+1);
}
The question is, WHY ON EARTH would someone do that?
EDIT
I forgot the end condition, which is now in place

How do I store a LONG list of names in a macro in Stata?

I need to store a very long list of variable names in Stata and after something like 250 characters, no more characters can be stored in a local or global macro. Currently, I'm using many different globals to store the names of the many regressors that I am using but I would much prefer to keep them all in one.
EDIT: The question has been perfectly answered by Maarten below but I would just add the code I was using for precision.
local RHSVARS = "var1 var2 var3 var4 var5 var6 var7 var8 var9 var10 var11 var12 var13 var14 var15 var16 var17 var18 var19"
does not work but
local RHSVARS "var1 var2 var3 var4 var5 var6 var7 var8 var9 var10 var11 var12 var13 var14 var15 var16 var17 var18 var19"
does.
This issue is largely solved in Stata 13, so I guess you have an older version.
You can still do so in older versions by just leaving out the equal sign, which you can see in the example below (it ran in Stata 12, in Stata 13 both macros are not truncated). This is discussed in the following article: Nicholas J. Cox (2008) "Stata tip 70: Beware the evaluating equal sign" The Stata Journal, 8(4): 586-587. It is now freely available here: http://www.stata-journal.com/article.html?article=pr0045
. // create local a with an equal sign
. local a = "`c(alpha)' `c(ALPHA)' `c(alpha)' `c(ALPHA)' `c(alpha)' `c(ALPHA)'"
. // create local b by leaving the equal sign out
. local b "`c(alpha)' `c(ALPHA)' `c(alpha)' `c(ALPHA)' `c(alpha)' `c(ALPHA)'"
.
. // local macro a gets truncated
. di `: length local a'
245
. di "`a'"
a b c d e f g h i j k l m n o p q r s t u v w x y z A B C D E F G H I J K L M N O P Q R S T U V W X
> Y Z a b c d e f g h i j k l m n o p q r s t u v w x y z A B C D E F G H I J K L M N O P Q R S T
> U V W X Y Z a b c d e f g h i j k l m n o p q r s
.
. // local macro b does not get truncated
. di `: length local b'
311
. di "`b'"
a b c d e f g h i j k l m n o p q r s t u v w x y z A B C D E F G H I J K L M N O P Q R S T U V W X
> Y Z a b c d e f g h i j k l m n o p q r s t u v w x y z A B C D E F G H I J K L M N O P Q R S T
> U V W X Y Z a b c d e f g h i j k l m n o p q r s t u v w x y z A B C D E F G H I J K L M N O P Q
> R S T U V W X Y Z

Resources